Search Unity

Need help with a scripting error.

Discussion in 'Scripting' started by dmb32693, Feb 17, 2014.

  1. dmb32693

    dmb32693

    Joined:
    Feb 17, 2014
    Posts:
    25
    I have this error when I was working with the stealth project cause I am new to unity.

    NullReferenceException: Object reference not set to an instance of an object
    LastPlayerSighting.Awake () (at Assets/Scripts/LastPlayerSighting.cs:23)


    Here is my script it points at alarm = GameObject.FindGameObjectWithTag(Tags.alarm).GetComponent<AlarmLight>();

    But hoping someone can help out.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LastPlayerSighting : MonoBehaviour
    5. {
    6.     public Vector3 position = new Vector3(1000f, 1000f, 1000f);         // The last global sighting of the player.
    7.     public Vector3 resetPosition = new Vector3(1000f, 1000f, 1000f);    // The default position if the player is not in sight.
    8.     public float lightHighIntensity = 0.25f;                            // The directional light's intensity when the alarms are off.
    9.     public float lightLowIntensity = 0f;                                // The directional light's intensity when the alarms are on.
    10.     public float fadeSpeed = 7f;                                        // How fast the light fades between low and high intensity.
    11.     public float musicFadeSpeed = 1f;                                   // The speed at which the
    12.    
    13.    
    14.     private AlarmLight alarm;                                           // Reference to the AlarmLight script.
    15.     private Light mainLight;                                            // Reference to the main light.
    16.     private AudioSource panicAudio;                                     // Reference to the AudioSource of the panic msuic.
    17.     private AudioSource[] sirens;                                       // Reference to the AudioSources of the megaphones.
    18.    
    19.    
    20.     void Awake ()
    21.     {
    22.         // Setup the reference to the alarm light.
    23.         alarm = GameObject.FindGameObjectWithTag(Tags.alarm).GetComponent<AlarmLight>();
    24.        
    25.         // Setup the reference to the main directional light in the scene.
    26.         mainLight = GameObject.FindGameObjectWithTag(Tags.mainLight).light;
    27.        
    28.         // Setup the reference to the additonal audio source.
    29.         panicAudio = transform.Find("secondaryMusic").audio;
    30.        
    31.         // Find an array of the siren gameobjects.
    32.         GameObject[] sirenGameObjects = GameObject.FindGameObjectsWithTag(Tags.siren);
    33.        
    34.         // Set the sirens array to have the same number of elements as there are gameobjects.
    35.         sirens = new AudioSource[sirenGameObjects.Length];
    36.        
    37.         // For all the sirens allocate the audio source of the gameobjects.
    38.         for(int i = 0; i < sirens.Length; i++)
    39.         {
    40.             sirens[i] = sirenGameObjects[i].audio;
    41.         }
    42.     }
    43.    
    44.    
    45.     void Update ()
    46.     {
    47.         // Switch the alarms and fade the music.
    48.         SwitchAlarms();
    49.         MusicFading();
    50.     }
    51.    
    52.    
    53.     void SwitchAlarms ()
    54.     {
    55.         // Set the alarm light to be on or off.
    56.         alarm.alarmOn = position != resetPosition;
    57.        
    58.         // Create a new intensity.
    59.         float newIntensity;
    60.        
    61.         // If the position is not the reset position...
    62.         if(position != resetPosition)
    63.             // ... then set the new intensity to low.
    64.             newIntensity = lightLowIntensity;
    65.         else
    66.             // Otherwise set the new intensity to high.
    67.             newIntensity = lightHighIntensity;
    68.        
    69.         // Fade the directional light's intensity in or out.
    70.         mainLight.intensity = Mathf.Lerp(mainLight.intensity, newIntensity, fadeSpeed * Time.deltaTime);
    71.        
    72.         // For all of the sirens...
    73.         for(int i = 0; i < sirens.Length; i++)
    74.         {
    75.             // ... if alarm is triggered and the audio isn't playing, then play the audio.
    76.             if(position != resetPosition  !sirens[i].isPlaying)
    77.                 sirens[i].Play();
    78.             // Otherwise if the alarm isn't triggered, stop the audio.
    79.             else if(position == resetPosition)
    80.                 sirens[i].Stop();
    81.         }
    82.     }
    83.    
    84.    
    85.     void MusicFading ()
    86.     {
    87.         // If the alarm is not being triggered...
    88.         if(position != resetPosition)
    89.         {
    90.             // ... fade out the normal music...
    91.             audio.volume = Mathf.Lerp(audio.volume, 0f, musicFadeSpeed * Time.deltaTime);
    92.            
    93.             // ... and fade in the panic music.
    94.             panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);
    95.         }
    96.         else
    97.         {
    98.             // Otherwise fade in the normal music and fade out the panic music.
    99.             audio.volume = Mathf.Lerp(audio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);
    100.             panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0f, musicFadeSpeed * Time.deltaTime);
    101.         }
    102.     }
    103. }
     
    Last edited: Feb 17, 2014
  2. dmb32693

    dmb32693

    Joined:
    Feb 17, 2014
    Posts:
    25
    Anyone at all?
     
  3. proandrius

    proandrius

    Unity Technologies

    Joined:
    Dec 4, 2012
    Posts:
    544
    Please use CODE /CODE, my brain hurts.
     
  4. dmb32693

    dmb32693

    Joined:
    Feb 17, 2014
    Posts:
    25
    How do I use /code? Where do I put it I'm new to this.
     
  5. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    My best guess would be there are no gameobjects with that tag, so FindGameObjectWithTag returns null.

    Double check that Tags.alarm is what you think it is, and that the things you think are tagged with that are also what you think they are.


    And you use code tags like this:
    [ code ]
    THIS IS WHERE THE CODE GOES
    [ /code ]

    only without the extra spaces.
    Code (csharp):
    1. THIS IS WHERE THE CODE GOES
     
  6. dmb32693

    dmb32693

    Joined:
    Feb 17, 2014
    Posts:
    25
    Would I turn FindGameObjectWithTag and put null next to it? Cause everything seems to be in order.
     
  7. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
  8. dmb32693

    dmb32693

    Joined:
    Feb 17, 2014
    Posts:
    25
    Thanks hope this is better now.
     
  9. dmb32693

    dmb32693

    Joined:
    Feb 17, 2014
    Posts:
    25
    Everything seems to be in order I even followed the tutorial for this project a long with what the devs say and others did to and experienced the same thing. But everything seems okay. Anyone gotta explanation?
     
  10. proandrius

    proandrius

    Unity Technologies

    Joined:
    Dec 4, 2012
    Posts:
    544
    Can you try using "alarm" instead of Tags.alarm and "mainLight" instead of Tags.mainLight. As well as for others (if I missed any).
     
  11. dmb32693

    dmb32693

    Joined:
    Feb 17, 2014
    Posts:
    25
    It just gives me more errors if I do that.
     
  12. proandrius

    proandrius

    Unity Technologies

    Joined:
    Dec 4, 2012
    Posts:
    544
    Did you put "" ?
     
  13. dmb32693

    dmb32693

    Joined:
    Feb 17, 2014
    Posts:
    25
    Oh wait hold on then.
     
  14. dmb32693

    dmb32693

    Joined:
    Feb 17, 2014
    Posts:
    25
    It fixes that other error but gave me this one UnityException: Tag: alarm is not defined!
    LastPlayerSighting.Awake () (at Assets/Scripts/LastPlayerSighting.cs:23)

    This is what the new line looks like.

    Code (csharp):
    1. alarm = GameObject.FindGameObjectWithTag("alarm").GetComponent<AlarmLight>();
     
  15. proandrius

    proandrius

    Unity Technologies

    Joined:
    Dec 4, 2012
    Posts:
    544
    Yeah, so the problem is that you don't have Tag defined as "alarm" in the Editor.
     
  16. dmb32693

    dmb32693

    Joined:
    Feb 17, 2014
    Posts:
    25
    So how do I fix this?
     
  17. proandrius

    proandrius

    Unity Technologies

    Joined:
    Dec 4, 2012
    Posts:
    544
  18. dmb32693

    dmb32693

    Joined:
    Feb 17, 2014
    Posts:
    25
    I know how to switch tags but what do I switch out?
     
  19. dmb32693

    dmb32693

    Joined:
    Feb 17, 2014
    Posts:
    25
    Have any ideas? Idk what to tag.
     
    Last edited: Feb 17, 2014
  20. dmb32693

    dmb32693

    Joined:
    Feb 17, 2014
    Posts:
    25
    It always points at my gameController
     
  21. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    The AlarmLight prefab should be tagged Alarm (looks like you are using alarm)
     
  22. dmb32693

    dmb32693

    Joined:
    Feb 17, 2014
    Posts:
    25
    What do you mean? AlarmLight is a script and can't be tagged?
     
  23. dmb32693

    dmb32693

    Joined:
    Feb 17, 2014
    Posts:
    25
    Thank you I just had to fix the other thanks so much all of you!
     
  24. TheCoryHarrell

    TheCoryHarrell

    Joined:
    Apr 16, 2014
    Posts:
    1
    Could someone clarify exactly what you did in the inspector or this line of code to fix the Null Exception?
    Code (csharp):
    1.  
    2.         alarm = GameObject.FindGameObjectWithTag(Tags.alarm).GetComponent<AlarmLight>();
    3.  
     
  25. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    In the inspector of the GameObject which has the AlarmLight component, dmb32693 changed the Tag to "Alarm"
     
  26. MadMathias

    MadMathias

    Joined:
    Oct 1, 2014
    Posts:
    2
    So to put it simply. The person who did the tutorial has done it all wrong? Calling the References to the Tags Script fails. So it is actually better to just call the actual tags in the inspector. It has taken me two days to find this out. I had even copied the script from the website and replaced my own thinking there was something I missed in his fast talking.
     
  27. JonIrenicus

    JonIrenicus

    Joined:
    Jan 25, 2016
    Posts:
    4
    It is too late for you, but it could help others...
    You have to add Tag "AlarmLight" to the "light_alarm_directional". It helps me :)