Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Flashlight error

Discussion in 'Editor & General Support' started by sesalpinogamer, Apr 10, 2015.

  1. sesalpinogamer

    sesalpinogamer

    Joined:
    Apr 25, 2013
    Posts:
    13
    Hello guys!I'm making a horror game and i'm messing with a realistic flashlight.I tried to find a good flashlight + battery life indicator,i searched for an hour and the most functional was from this vid:

    And i inserted the code and followed EVERYTHING,here is it:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var flashlightLightSource : Light;
    4. var lightOn : boolean = true;
    5. var lightDrain : float = 0.1;
    6. private static var batteryLife : float = 0.0;
    7. var maxBatteryLife : float = 2.0;
    8.  
    9. private static var batteryPower : float = 1;
    10.  
    11. var barDisplay : float = 0;
    12. var pos : Vector2 = new Vector2(20,40);
    13. var size : Vector2 = new Vector2(60,20);
    14. var progressBarEmpty : Texture2D;
    15. var progressBarFull : Texture2D;
    16.  
    17. var soundTurnOn : AudioClip;
    18. var soundTurnOff : AudioClip;
    19.  
    20.  
    21. function Start()
    22. {
    23.     batteryLife = maxBatteryLife;
    24.     flashlightLightSource = GetComponent(Light);
    25. }
    26.  
    27.  
    28. static function AlterEnergy (amount : int)
    29. {
    30.     batteryLife = Mathf.Clamp(batteryLife+batteryPower, 0, 100);
    31.  
    32. }
    33.  
    34.  
    35.  
    36. function Update()
    37. {
    38.  
    39. //BATTERY LIFE BRIGHTNESS//////////
    40.     if(lightOn && batteryLife >= 0)
    41.     {
    42.         batteryLife -= Time.deltaTime * lightDrain;
    43.     }
    44.         if(lightOn && batteryLife <= 0.4)
    45.     {
    46.         flashlightLightSource.light.intensity = 5;
    47.     }
    48.         if(lightOn && batteryLife <= 0.3)
    49.     {
    50.         flashlightLightSource.light.intensity = 4;
    51.     }
    52.     if(lightOn && batteryLife <= 0.2)
    53.     {
    54.         flashlightLightSource.light.intensity = 3;
    55.     }
    56.         if(lightOn && batteryLife <= 0.1)
    57.     {
    58.         flashlightLightSource.light.intensity = 2;
    59.     }
    60.             if(lightOn && batteryLife <= 0)
    61.     {
    62.         flashlightLightSource.light.intensity = 0;
    63.     }
    64.    
    65.  
    66.    
    67.     barDisplay = batteryLife;
    68.    
    69.     if(batteryLife <= 0)
    70.     {
    71.         batteryLife = 0;
    72.         lightOn = false;
    73.     }
    74.    
    75.     if(Input.GetKeyUp(KeyCode.F))
    76.     {
    77.         toggleFlashlight();
    78.         toggleFlashlightSFX();
    79.        
    80.         if(lightOn)
    81.         {
    82.             lightOn = false;
    83.         }
    84.         else if (!lightOn && batteryLife >= 0)
    85.         {
    86.             lightOn = true;
    87.         }
    88.     }
    89. }
    90.    
    91.     /////// PIC  ///////////
    92. function OnGUI()
    93. {
    94.     // draw the background:
    95.     GUI.BeginGroup (new Rect (pos.x, pos.y, size.x, size.y));
    96.         GUI.Box (Rect (0,0, size.x, size.y),progressBarEmpty);
    97.         // draw the filled-in part:
    98.         GUI.BeginGroup (new Rect (0, 0, size.x * barDisplay, size.y));
    99.             GUI.Box (Rect (0,0, size.x, size.y),progressBarFull);
    100.         GUI.EndGroup ();
    101.     GUI.EndGroup ();
    102. }
    103.    
    104. function toggleFlashlight()
    105. {
    106.     if(lightOn)
    107.     {
    108.         flashlightLightSource.enabled = false;
    109.     }
    110.     else
    111.     {
    112.         flashlightLightSource.enabled = true;
    113.     }
    114. }
    115. function toggleFlashlightSFX()
    116. {
    117.     if(flashlightLightSource.enabled)
    118.     {
    119.         audio.clip = soundTurnOn;
    120.     }
    121.     else
    122.     {
    123.         audio.clip = soundTurnOff;
    124.     }
    125.     audio.Play();
    126.    
    127. }
    128.  
    129.     @script RequireComponent(AudioSource)
    130.  
    131. ////////You are welcome/////////
    But for some reason,when i turn off the flashlight,to preserve my battery,the error was that when i turned my flashlight off unity crashed,but i just changed the flashlight location from main camera to firstpersoncontroller.But now,it does turn off,and work properly except that THIS happens in my console:

    MissingComponentException: There is no 'Light' attached to the "First Person Controller" game object, but a script is trying to access it.
    You probably need to add a Light to the game object "First Person Controller". Or your script needs to check if the component is attached before using it.
    Flashy.toggleFlashlight () (at Assets/Standard Assets/Flashy.js:112)
    Flashy.Update () (at Assets/Standard Assets/Flashy.js:77)


    Anyone know the issue?,I'm using unity 4.6.2 because unity 5 glitched the render setting so i cannot make it night or foggy,which is the version ryandome used,and also,my flashlight keeps on after the error,yet it still displays the code.
     
  2. DudoTheStampede

    DudoTheStampede

    Joined:
    Mar 16, 2015
    Posts:
    79
    Only issue I can think of is repeated script in your hierarchy...
    In the First Person Controller game object (where you should have your Flash.js), do you have a Light? That's the reason why you get the error! In line 24 you do a GetComponent, if you dont have a Light Component in the same gameobject as the script, it'll be a null variable and so the error you see in console!

    You said that the light goes on/off, but I'm pretty sure that's because you have another Flash.js as component of the gameobject where you do have the Light component now!
     
  3. sesalpinogamer

    sesalpinogamer

    Joined:
    Apr 25, 2013
    Posts:
    13
    Thanks!I'll try
     
  4. sesalpinogamer

    sesalpinogamer

    Joined:
    Apr 25, 2013
    Posts:
    13
    It works!!!!!!!! I just deleted the script from the firstpersoncontroller and put it directly on my flashlight,thank you.
     
  5. sesalpinogamer

    sesalpinogamer

    Joined:
    Apr 25, 2013
    Posts:
    13
    Actually,my flashlight only looks forward,not up or down,but anyways