Reloading Scene Causing Null Refference
Hello guys, i m making a FPS game. but whenever i load the scene 2nd time, i get alot of Null References. I know it has something to do with static variables used in 1 or 2 script files or game objects that do not destroy. But, my project has alot of files and somehow i cannot seem to understand the reason behind this issue.
If you want to go to the problem state here are the steps.
Start The Game
Press Escape
Press Yes
Press Start
Look into console Window (You Will See : MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.)
Here is the complete project in Zip Format : https://archive.org/download/abc_TestGameArchive/Test.zip
Kindly download the project and take a look. It will be much appreciated. I am a beginner and still learning things.
Answer by tormentoarmagedoom · May 07, 2018 at 10:57 AM
Good day.
If you are receiveng a NullReferenceException, is because some variable is not assigned when trying to access it.
You must track the issue by debuging the code while executing it, discover what variable is null when try to access it,m and then discover why is null.
If you are using DontDestroyOnLoad, make sure the objects are not duplicated, or you are not deleting the object that try to acess..
Bye!
i m not using DontDestroyOnLoad in my scripts. Here is the album that explains the issue : https://imgur.com/a/QwBDVOd and this is my complete Game$$anonymous$$anager Script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Scene$$anonymous$$anagement; public class Game$$anonymous$$anager {
public event System.Action<Player> OnLocalPlayerJoined;
private GameObject goo;
public bool isPaused { get; set; }
private static Game$$anonymous$$anager m_instance;
public static Game$$anonymous$$anager Instance
{
get {
if (m_instance == null) {
m_instance = new Game$$anonymous$$anager();
m_instance.goo = new GameObject("_game$$anonymous$$anager");
m_instance.goo.AddComponent<InputController>();
m_instance.goo.AddComponent<Timer>();
m_instance.goo.AddComponent<Respawner>();
}
return m_instance;
}
}
private InputController ic;
public InputController inputController{
get{
if (ic == null) {
ic = goo.GetComponent<InputController>();
}
return ic;
}
} private Timer m_timer; public Timer Timer { get { if (m_timer == null) { m_timer = goo.GetComponent();
}
return m_timer;
}
}
private EventBus m_EventBus;
public EventBus EventBus
{
get
{
if (m_EventBus == null)
{
m_EventBus = new EventBus();
}
return m_EventBus;
}
}
private Respawner m_respawner;
public Respawner Respawner {
get {
if (m_respawner == null) {
m_respawner = goo.GetComponent<Respawner>();
}
return m_respawner;
}
}
private Player pl;
public Player localPlayer {
get {
return pl;
}
set {
pl = value;
if (OnLocalPlayerJoined != null) {
OnLocalPlayerJoined(pl);
}
}
}
}
I tell you the same, some variables are not assigned (oe foir every error you get).
For example in one image i see you get an error for this line:
something = goo.GetComponent();
If get null reference is because:
Or object goo is not defined, you didnt tell Unity what object is goo..
or
goo object does not have a component called Timer.
IF you are sure one this them is not the problem, the problem is the other thing... Explore! Debug the code while executing, and check the states of the variables right before beeing execute! Create Debug.Log with all variables before beeing executed, to see via console whats the state (some of them will be null) !
Bye!!
Your answer
Follow this Question
Related Questions
Object not visible in Scene tab until double click it on the Hierarchy. 1 Answer
win game & loos game 0 Answers
How can I detect if a user is looking at a Gameobject inside a scene from a VR headset 0 Answers
Game Objects not using same script independently. 1 Answer
Help me! Create a game object falling from the top scene at click mouse point! T________T 0 Answers