- Home /
DontDestroyOnLoad with object on another scene
So I have an Options UI on my main menu and a pause UI on my level1. I want to make it so when I press the back button, it will go to my pause menu. That currently doesnt work. right now I am using "GameObject.Find("pause").SetActive(true);" and it isnt working I get an error saying: "NullReferenceException: Object reference not set to an instance of an object optionsprompt.BackButton (UnityEngine.Object myObject) (at Assets/optionsprompt.cs:67)" line 67 is the code I showed. Why is this happening and is there a better way of doing this?
Sounds like there isn't a gameobject in your scene called "pause"...
Answer by skylem · Jan 14, 2015 at 10:22 PM
My best advice would be to create a GameManager Gameobject that will hold all your data for your player and his activitys and then u would use this gameobject to activate your pause menu for you. for example.
public GameObject pauseMenu; // drag your pause menu object into here
public bool paused; // This is your off switch
if(Input.GetKeyDown(Keycode.Escape) && paused == false) {
pauseMenu.SetActive(true);
paused = true;
}
else if(Input.GetKeyDown(Keycode.Escape) && paused == true) {
pauseMenu.SetActive(false);
paused = false;
}
hope this helps
I already figured it out but yes this was somewhat the issue that I didnt think to try out hahaha thanks for the response I shall accept this as the answer
Answer by Mondkin · Jan 14, 2015 at 10:13 PM
GameObject.find()
doesn't find inactive objects. That is why it is returning null
, so you better work with the enabled/disabled state of its elements (not the GameObject itself).
From the documentation:
If no game object with name can be found, null is returned. If name contains a '/' character it will traverse the hierarchy like a path name. This function only returns active gameobjects.
And by the way, you are not alone, this is a common catch and several people have wanted that feature to be added:
I figured out my issue by doing this: GameObject.Find("PauseFunctions").GetComponent().Pause$$anonymous$$enu.SetActive(true); I never thought to try that. $$anonymous$$any thanks for the help though I shall give +rep