Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 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
Add comment
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

· Add your reply
  • Sort: 
avatar image
1
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
Add comment · 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 program$$anonymous$$g 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() ins$$anonymous$$d of Awake(). I edited my comment to make it clear.

Show more comments
avatar image
0

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
Add comment · 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.

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.

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

Distortion spatial 3d sound when moving and rotating FPS Controller 1 Answer

need help to executing sound 3 Answers

footstep sound affect in the air 1 Answer


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