• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dmb32693 · Feb 17, 2014 at 06:44 PM · script error

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

Here is my script I was working with the stealth project but it gives me that error when playing the music.

 using UnityEngine;
 using System.Collections;
 
 public class LastPlayerSighting : MonoBehaviour
 {
     public Vector3 position = new Vector3(1000f, 1000f, 1000f);         // The last global sighting of the player.
     public Vector3 resetPosition = new Vector3(1000f, 1000f, 1000f);    // The default position if the player is not in sight.
     public float lightHighIntensity = 0.25f;                            // The directional light's intensity when the alarms are off.
     public float lightLowIntensity = 0f;                                // The directional light's intensity when the alarms are on.
     public float fadeSpeed = 7f;                                        // How fast the light fades between low and high intensity.
     public float musicFadeSpeed = 1f;                                   // The speed at which the 
     
     
     private AlarmLight alarm;                                           // Reference to the AlarmLight script.
     private Light mainLight;                                            // Reference to the main light.
     private AudioSource panicAudio;                                     // Reference to the AudioSource of the panic msuic.
     private AudioSource[] sirens;                                       // Reference to the AudioSources of the megaphones.
     
     
     void Awake ()
     {
         // Setup the reference to the alarm light.
         alarm = GameObject.FindGameObjectWithTag(Tags.alarm).GetComponent<AlarmLight>();
         
         // Setup the reference to the main directional light in the scene.
         mainLight = GameObject.FindGameObjectWithTag(Tags.mainLight).light;
         
         // Setup the reference to the additonal audio source.
         panicAudio = transform.Find("secondaryMusic").audio;
         
         // Find an array of the siren gameobjects.
         GameObject[] sirenGameObjects = GameObject.FindGameObjectsWithTag(Tags.siren);
         
         // Set the sirens array to have the same number of elements as there are gameobjects.
         sirens = new AudioSource[sirenGameObjects.Length];
         
         // For all the sirens allocate the audio source of the gameobjects.
         for(int i = 0; i < sirens.Length; i++)
         {
             sirens[i] = sirenGameObjects[i].audio;
         }
     }
     
     
     void Update ()
     {
         // Switch the alarms and fade the music.
         SwitchAlarms();
         MusicFading();
     }
     
     
     void SwitchAlarms ()
     {
         // Set the alarm light to be on or off.
         alarm.alarmOn = position != resetPosition;
         
         // Create a new intensity.
         float newIntensity;
         
         // If the position is not the reset position...
         if(position != resetPosition)
             // ... then set the new intensity to low.
             newIntensity = lightLowIntensity;
         else
             // Otherwise set the new intensity to high.
             newIntensity = lightHighIntensity;
         
         // Fade the directional light's intensity in or out.
         mainLight.intensity = Mathf.Lerp(mainLight.intensity, newIntensity, fadeSpeed * Time.deltaTime);
         
         // For all of the sirens...
         for(int i = 0; i < sirens.Length; i++)
         {
             // ... if alarm is triggered and the audio isn't playing, then play the audio.
             if(position != resetPosition && !sirens[i].isPlaying)
                 sirens[i].Play();
             // Otherwise if the alarm isn't triggered, stop the audio.
             else if(position == resetPosition)
                 sirens[i].Stop();
         }
     }
     
     
     void MusicFading ()
     {
         // If the alarm is not being triggered...
         if(position != resetPosition)
         {
             // ... fade out the normal music...
             audio.volume = Mathf.Lerp(audio.volume, 0f, musicFadeSpeed * Time.deltaTime);
             
             // ... and fade in the panic music.
             panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);
         }
         else
         {
             // Otherwise fade in the normal music and fade out the panic music.
             audio.volume = Mathf.Lerp(audio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);
             panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0f, musicFadeSpeed * Time.deltaTime);
         }
     }
 }
Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dmb32693 · Feb 17, 2014 at 09:29 PM 0
Share

alarm = GameObject.FindGameObjectWithTag(Tags.alarm).GetComponent();

is what it points at

avatar image DajBuzi · Feb 17, 2014 at 09:38 PM 0
Share

You need reference to Tags object.

avatar image dmb32693 · Feb 17, 2014 at 09:41 PM 0
Share

Can you show me how to do that?

avatar image DajBuzi · Feb 17, 2014 at 09:47 PM 0
Share

You are trying to access Tags object and you doesnt have ref to this.

 Tags tags = GetComponent<Tags>();

//or just use strings ins$$anonymous$$d of another object

avatar image dmb32693 · Feb 17, 2014 at 09:52 PM 0
Share

So where would I put this?

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by unimechanic · Feb 25, 2014 at 07:05 PM

The code provided is incomplete, that specific error is not appearing.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by unimechanic · Feb 25, 2014 at 07:06 PM

[Question answered, adding this to remove it from Unanswered list]

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by ZAD44 · Feb 12, 2015 at 12:40 AM

use this code just copy paste,

using UnityEngine; using System.Collections;

using UnityEngine; using System.Collections;

public class LastPlayerSighting : MonoBehaviour { public Vector3 position = new Vector3(1000f, 1000f, 1000f); // The last global sighting of the player. public Vector3 resetPosition = new Vector3(1000f, 1000f, 1000f); // The default position if the player is not in sight. public float lightHighIntensity = 0.25f; // The directional light's intensity when the alarms are off. public float lightLowIntensity = 0f; // The directional light's intensity when the alarms are on. public float fadeSpeed = 7f; // How fast the light fades between low and high intensity. public float musicFadeSpeed = 1f; // The speed at which the

 private AlarmLight alarm;                                           // Reference to the AlarmLight script.
 private Light mainLight;                                            // Reference to the main light.
 private AudioSource panicAudio;                                     // Reference to the AudioSource of the panic msuic.
 private AudioSource[] sirens;                                       // Reference to the AudioSources of the megaphones.


 void Awake()
 {
     // Setup the reference to the alarm light.
     alarm = GameObject.FindGameObjectWithTag(Tags.alarm).GetComponent<AlarmLight>();

     // Setup the reference to the main directional light in the scene.
     mainLight = GameObject.FindGameObjectWithTag(Tags.mainLight).light;

     // Setup the reference to the additonal audio source.
     panicAudio = transform.Find("secondaryMusic").audio;

     // Find an array of the siren gameobjects.
     GameObject[] sirenGameObjects = GameObject.FindGameObjectsWithTag(Tags.siren);

     // Set the sirens array to have the same number of elements as there are gameobjects.
     sirens = new AudioSource[sirenGameObjects.Length];

     // For all the sirens allocate the audio source of the gameobjects.
     for (int i = 0; i < sirens.Length; i++)
     {
         sirens[i] = sirenGameObjects[i].audio;
     }
 }


 void Update()
 {
     // Switch the alarms and fade the music.
     SwitchAlarms();
     MusicFading();
 }


 void SwitchAlarms()
 {
     // Set the alarm light to be on or off.
     alarm.alarmOn = position != resetPosition;

     // Create a new intensity.
     float newIntensity;

     // If the position is not the reset position...
     if (position != resetPosition)
         // ... then set the new intensity to low.
         newIntensity = lightLowIntensity;
     else
         // Otherwise set the new intensity to high.
         newIntensity = lightHighIntensity;

     // Fade the directional light's intensity in or out.
     mainLight.intensity = Mathf.Lerp(mainLight.intensity, newIntensity, fadeSpeed * Time.deltaTime);

     // For all of the sirens...
     for (int i = 0; i < sirens.Length; i++)
     {
         // ... if alarm is triggered and the audio isn't playing, then play the audio.
         if (position != resetPosition && !sirens[i].isPlaying)
             sirens[i].Play();
         // Otherwise if the alarm isn't triggered, stop the audio.
         else if (position == resetPosition)
             sirens[i].Stop();
     }
 }


 void MusicFading()
 {
     // If the alarm is not being triggered...
     if (position != resetPosition)
     {
         // ... fade out the normal music...
         audio.volume = Mathf.Lerp(audio.volume, 0f, musicFadeSpeed * Time.deltaTime);

         // ... and fade in the panic music.
         panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);
     }
     else
     {
         // Otherwise fade in the normal music and fade out the panic music.
         audio.volume = Mathf.Lerp(audio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);
         panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0f, musicFadeSpeed * Time.deltaTime);
     }
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What's Wrong With This Script? 1 Answer

When attempting to call a method from within another script I get this error. 3 Answers

get component screws with the game. 2 Answers

error CS1729 1 Answer

Error CS0266: Cannot implicitly convert type `double' to `int'. An explicit conversion exists (are you missing a cast?) 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges