• 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
Question by Gng357 · Dec 06, 2018 at 12:00 PM · soundnullreferenceexceptiononlevelwasloaded

NullReferenceException only at first run

This code works, but I get the following message:

"NullReferenceException: Object reference not set to an instance of an object SoundManager.OnLevelFinishedLoading (Scene scene, LoadSceneMode mode)"

I only get it at first run (Yes, I have the gameobject attached in first scene). Then in every other scene everything is fine, no error message. Also even if I go back to the first scene I don't get the message back. It works but I am trying to understand why this happens only when the game loads for the first time.

 public class SoundManager : MonoBehaviour {
 
     public AudioClip[] levelMusicChangeArray;
     private AudioSource audioSource;
 
     public static SoundManager Instance;
 
     private void Awake()
     {      
         if (Instance == null)
             Instance = this;
       
         else if (Instance != this)
             Destroy(gameObject);
       
         DontDestroyOnLoad(gameObject);
     }
 
     private void Start()
     {
         audioSource = GetComponent<AudioSource>();
         audioSource.volume = PlayerPrefsManager.GetMasterVolume();
     }
 
     private void OnEnable()
     {
         SceneManager.sceneLoaded += OnLevelFinishedLoading;
     }
     
     private void OnDisable()
     {
         SceneManager.sceneLoaded -= OnLevelFinishedLoading;
     }
 
     private void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode)
     {
         int level = scene.buildIndex;
         AudioClip thisLevelMusic = levelMusicChangeArray[level];
 
         try
         {
             if (thisLevelMusic)
             {
                 audioSource.clip = thisLevelMusic;
                 audioSource.loop = true;
                 audioSource.Play();
             }
         }
         catch (NullReferenceException e)
         {
             Debug.LogException(e);
         }       
     }
     
 }



Comment

People who like this

0 Show 0
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

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by hexagonius · Dec 06, 2018 at 12:09 PM

that's because OnEnable is called prior to start and in turn receives the OnLevelFinishedLoading call prior to start sich is itself called before Start's are called. And since you use the singleton pattern, after the first NullReferenceException, it knows it's reference and the problem won't appear again.
as a fix and future recommendations, do GetComponent calls performed on itself in Awake.

Comment
Gng357

People who like this

1 Show 4 · 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 Gng357 · Dec 06, 2018 at 12:22 PM 0
Share

Thank you for your answer @hexagonius. I changed the order, from Start() to Awake(). What I mean is that I put audioSource = GetComponent(); in Awake() and it worked!

Thank you!

avatar image hexagonius Gng357 · Dec 06, 2018 at 12:39 PM 0
Share

if you meant changing the order of how the methods are written in the file, then you should have a course in programming really quick.

avatar image Gng357 hexagonius · Dec 06, 2018 at 01:03 PM 0
Share

No, what I meant is the order in life cycle that I had it GetComponent in Start() instead of Awake(). I edited my comment to make it clear.

Show more comments
avatar image

Answer by R4y-GM · Dec 06, 2018 at 12:11 PM

u selected the prefabs?

          catch (NullReferenceException e)
          {
              Debug.LogException(e);
          }       //what this does?  I do not understand
Comment

People who like this

0 Show 1 · 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 Gng357 · Dec 06, 2018 at 12:23 PM 0
Share

I just catch the exception here.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

101 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Nullreference exception after creating asset 1 Answer

Adding sound to animation: NullReferenceException 0 Answers

Echo-location 0 Answers

Playing 5.1 surround audio 2 Answers

loop animation while audio is playing 0 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