Search Unity

Flashlight script error?

Discussion in 'Scripting' started by GamesOnAcid, Feb 12, 2016.

  1. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    My flashlight script requires a Light. I attach a light as a child of the FPSController (my character), and drag it into the inspector to set it as the script's light. When the game runs however, it goes blank and tells me that the light source is missing. Can anyone tell me why?
    Code (JavaScript):
    1. var flashlightLightSource : Light;
    2. var batteryLife : float = 20.0
    3. //How I call the light source
    4. function Start ()
    5. {
    6.     flashlightLightSource = GetComponent(Light);
    7. }
    8. //Getting the component
    9. function Update ()
    10. {
    11. flashlightLightSource.GetComponent(Light).intensity = batteryLife;
    12. }
    13. //Using it
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    the reference is of type Light and you populate it by dragging the light into the inspector slot.
    you then try to get the component of type light for it again in start which will look on the current gameobject, not the child.
    then when you try to access it again to check the intensity you try to find the light's light (it's already a light) which is again going to look on the current gameobject, not the child.

    You don't need the start, you don't need the GetComponent(light) in line 11.
     
  3. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    Works, thanks! Guess I thought that getting the intensity was a whole other object? I'm not sure. Worked however.