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
2
Question by ciwolsey · Oct 15, 2016 at 10:00 PM · scripting problemscenescene-loadingscriptingproblemreferences

Changing scene causes null reference exceptions

I have two perfectly working scenes I'd like to switch between, but whenever I do, I get a bunch of null reference exceptions. Can anyone explain why this happens?

A typical example is a script that plays an audio source. After reloading the current scene, or switching to a new one "audioSource" becomes null. Here is how I switch scenes:

 SceneManager.LoadScene("anotherScene");

And here is just one example of where a null reference exception occurs after changing scenes:

 using UnityEngine;
 using System.Collections;
 
 public class Sounds : MonoBehaviour {
 
     public AudioSource spraySound;
 
     void Start()
     {
         spraySound = GetComponent<AudioSource>();
     }
 
     void OnEnable()
     {
         spraycan.OnSprayStart += OnSprayStart;
         spraycan.OnSprayStop += OnSprayStop;
     }
 
     void OnSprayStart()
     {
         spraySound.Play(); // Null reference exception after reloading scene
     }
 
     void OnSprayStop()
     {
         spraySound.Stop(); // Null reference exception after reloading scene
     }
 }



Comment
Add comment · Show 1
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 darrensmith · Oct 06, 2017 at 08:01 PM 0
Share

Same problem here. Is there a solution?

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by doublemax · Oct 15, 2016 at 10:07 PM

First thing to do is checking if Start() is called and if GetComponent returns null there. Also check the call order. Is it possible that OnSprayStart is called before Start() is executed?

I don't know if it's relevant, but if you add callbacks in OnEnable you should remove them in OnDisable.

Comment
Add comment · Show 2 · 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 ciwolsey · Oct 15, 2016 at 10:54 PM 0
Share

Thanks for the help, I feel like I'm getting a more accurate picture of things now I'm removing the callbacks in OnDisable(). There's a lot of code, it'll take some time for me to work out if I really have solved the problem.

avatar image ciwolsey · Oct 16, 2016 at 01:01 AM 0
Share

After some more time it's pretty clear you're spot on. Checking for null in Start() and assigning if it is, as well making making sure all callbacks are deregistered fixed my game. If you want to post an answer repeating this I'll select it as the solution. Thanks again.

avatar image
0

Answer by dk0r · Oct 15, 2016 at 10:18 PM

I've been having a similar issue as well and have yet to resolve it. If you have a chance, please post your actual error(s).

Also, there are several posts on StackOverflow regarding this very issue but none of the suggested solutions have actually worked for me.

Comment
Add comment · Show 6 · 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 darrensmith · Oct 06, 2017 at 09:23 PM 0
Share

Did you ever find a solution? I'm having the same problem. I've unsubscribed in OnDisable, but yet when I reload the scene, the objects that were perfectly fine the first time I was in the scene are now null.

avatar image MaxGuernseyIII darrensmith · Oct 06, 2017 at 09:31 PM 0
Share

@doublemax Posted the solution as a comment on the question. @ciwolsey said that he would accept the answer if @doublemax posted it as an answer.

avatar image FortisVenaliter MaxGuernseyIII · Oct 06, 2017 at 09:33 PM 0
Share

$$anonymous$$oved the comment to an answer and accepted it for future users.

Show more comments
avatar image
0

Answer by darrensmith · Oct 06, 2017 at 10:20 PM

Found the problem. I missed disabling one callback. I'm using GameSparks. It was the message listener, catching all of the messages from the server:

 void OnDisable() {
      GameSparks.Api.Messages.ScriptMessage.Listener -= GetMessages;
 }

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.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Bolt Visual scripting and loading different scenes/levels 1 Answer

"Scene couldn't be loaded because it isn't added to the build settings" but it is? 1 Answer

How do I preload multiple scenes at once? 0 Answers

How to load a scene with GameFlow on button click ? ( XR RIG / Oculus Quest ) 0 Answers

Keeping reference after scene reload 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