- Home /
Can't use variable after scene reload
My current project is set up such that I have 3 scenes, a main menu scene, a scene where the user plays the game, and then a highscore scene. I have it so that from the highscore screen you can click back to the main menu and it will load that scene. The problem is that if I try and use the buttons on the main menu I get a "GameObject has been deleted" missing reference error. I've narrowed it down to the fact that in my menu (I'm using EZ GUI, although it doesn't appear to be ez gui because the messages are getting through) I SetActiveRecursively(false) the panels that I am not using and SetActiveRecursively(true) the one that I am using. So if the user clicks on the instructions button all of the other panels except the instructions panel get deactivated and the instructions panel gets activated.
The exact error occurs on one of those lines, a mainMenuPanel_.SAR(true); line. I thought maybe initially it was because my public GameObject references had lost their reference, but the problem persists even if I load in those references on Awake or Start through code. Has anyone seen anything like this before? If I make a new project and have 2 scenes that link back and forth to each other with buttons, I don't seem to have this problem, so it must be something subtle that I'm doing.
Any thoughts? Thanks in advance for any responses.
EDIT: If I debug out the references in awake and start they appear to be fine, but the reference is null in other functions in that same script even though it's using the same reference as start and awake. Even if I try and reinitialize the variable in this function (just to see if it works) it errors. It's like the reference itself is invalid, even if I try and reassign it.
below is my code:
using UnityEngine;
using System.Collections;
public class MenuController : MonoBehaviour
{
private GameObject mainMenuPanel_;
private GameObject interfacePanel_;
private GameObject instructionsPanel_;
private void Awake()
{
//this works fine as well, interacting with the panels that I have defined globally above.
mainMenuPanel_ = transform.FindChild("MainMenu").gameObject;
interfacePanel_ = transform.FindChild("InterfaceMenu").gameObject;
instructionsPanel_ = transform.FindChild("InstructionsMenu").gameObject;
MainMenuResponder.interfaceHit += switchToInterface;
MainMenuResponder.instructionsHit += switchToInstructions;
InterfaceMenuResponder.backHit += switchToMain;
InstructionsMenuResponder.backHit += switchToMain;
}
private void Start()
{
//This works fine, also interacting with the panels.
switchToMain();
}
private void switchToInterface()
{
mainMenuPanel_.SetActiveRecursively(false);
interfacePanel_.SetActiveRecursively(true);
instructionsPanel_.SetActiveRecursively(false);
}
private void switchToMain()
{
mainMenuPanel_.SetActiveRecursively(true);
interfacePanel_.SetActiveRecursively(false);
instructionsPanel_.SetActiveRecursively(false);
}
private void switchToInstructions()
{
Debug.Log(mainMenuPanel_); //this is null the second time that I load it, despite it working in Start and Awake
mainMenuPanel_.SetActiveRecursively(false);
interfacePanel_.SetActiveRecursively(false);
instructionsPanel_.SetActiveRecursively(true);
}
}
I have them as private to make sure it wasn't a problem with public references, I originally had them as public and had just dragged the respective GameObjects onto the links in the inspector, but I wanted to make sure that it wasn't the problem. The events that I'm subscribing to in the Awake function are driven by scripts that are children of this GameObject and are in the same scene.
You do have DontDestroyOnLoad set on things that need to persist between scenes?
Yes, but I don't need this to be persisted between scenes. I just need to be able to use the variable and GameObject after I Application.LoadLevel(..). It's not a persistence issue, it's just menu buttons, etc. I don't need them in any other scene.
Well if you LoadLevel your objects will be destroyed... Unless they have DontDestroyOnLoad
I don't need to access any of these objects or scripts outside of their scene, they can be destroyed on load, but when I load the level it is supposed to reload the objects that are currently in that scene, and it doesn't appear that it is. For example, if I make a new project and make 3 scenes with buttons that switch around between the 3 scenes, all of the GameObjects and anything else in those scenes will get reloaded when I load that level and I'm not applying DontDestroyOnLoad to anything. I'm pretty sure DontDestroyOnLoad is in there for the case where I want to persist GameObjects and components across multiple scenes, not the case where I want things to be loaded when I call LoadLevel.
Very true. But you get that error if you still have a reference to an object in the previous scene and try to do something with it. In your question you mention calling SetActiveRecursively on something - so you think you have re-established that object to the one that has just been reloaded?
Your answer
Follow this Question
Related Questions
Deleted GameObject on Scene Load 0 Answers
Keep GameObject to variable linkage on LoadLevel 0 Answers
Change level upon clicking a gameobject 2 Answers
Weird GetComponent error 2 Answers