- Home /
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
}
}
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.
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.
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.
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.
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.
@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.
$$anonymous$$oved the comment to an answer and accepted it for future users.
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;
}
Your answer
Follow this Question
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