- Home /
Trouble with DontDestroyOnLoad
Hi Gurus,
I'm having a heck of a time with what should be a simple operation.
I'm using DontDestroyOnLoad(this.gameObject) on an empty object with scripts that I'm using as a game controller for variables, scores, etc. for the scope of the game.
The object, gameController, is loaded in the first scene and then I load the second scene and I'm attempting to access variables and scripts in gameController
I've put the following in the AWAKE and START functions in the script called "GameController" which is a component of the empty object called "gameController" [upper case G on the script, lower case g on the object - no particular reason] (tried AWAKE and START individually and together)
DontDestroyOnLoad(transform.gameObject);
DontDestroyOnLoad(this.gameObject);
DontDestroyOnLoad(this);
I think this is overkill but still not working.
Here is (hopefully the relevant portions of) the script in the second scene that I'm attempting to access it with:
var GC : GameObject; // gameController Object (lower case g)
var GCS : GameController; // GameController Script (upper case G)
[inside START function]
// Find GameController script and assign a local reference
if (GameObject.Find("gameController") == null){
Debug.Log("get returns null");
}
GC = GameObject.Find("gameController");
GCS = GC.GetComponent("GameController");
The null warning is issued in the Console and no access.
I've googled the life out of this and read many forum answers, and they all seem to point in the same direction that I believe that I'm already going.
Help in this is very much appreciated, as development is at a screeching halt...sigh
Thanks up front!
By the looks of your snippets, I'd venture a guess and say the GameObject that has the GameController script isn't named "gameController". Every one of those DontDestroyOnLoad you try should prevent the destruction of the gameobject... Is the controller object still in the scene hierarchy after the second scene loads?
Thanks for your response Jamoro, but yeah, the gameController object name is "gameController" (lower case g), and the script is called "GameController" (upper case G).
Real creative, I know.
Other ideas (please!)?
You wrote the Start() and Awake() functions in CAPS, did you write them in caps in your code as well? Only the first letter should be capitalized...
Thanks Jamora, but yeah, I don't know why I typed them in caps for this question, but they are correct in the script "Awake" and "Start".
Great detail catching, and I'll try to be more accurate, cause after all you guys are helping me look for the detail I'm missing. Thanks.
Answer by ErikBoyeAbrah · Jul 23, 2013 at 08:28 PM
(Edit)
I made a little demo to change Scenes as a test.
I took my EmptyGameObject(Manager) and made it a prefab by dragging it into a prefab folder I made in my assets.
I then made a new script C# called OnLoad.
using UnityEngine; using System.Collections;
public class OnLoad : MonoBehaviour {
void Awake(){ DontDestroyOnLoad(transform.gameObject); } }
Then I put the OnLoad script on my Manager prefab
When I then changed the scene my manager was still in the next scene, so this was a success.
I hope this helps.
(Edit)
I accidentally removed the link to the game state manager, her is it again. http://www.fizixstudios.com/labs/do/view/id/unity-game-state-manager
Thanks for your respone ErikBoyeAbrah,
I attempted to do as you suggested.
I went into the script and added this
public var game_controller : Transform;
Then went to the inspector and dropped the object "gameController" into it, however the second scene is still unable to "find" it.
I also added the "Static" check mark in the inspector of "gameController" in case I was missing something, and still nope.
Thank you very much, if you think of something else I would appreciate hearing about it.
When you go to your new scene is your gameobject in the Hierarchy?
Thanks ErikBoyeAbrah,
No, it isn't. I'm thinking that if it was it would be a different instance of the object and whatever variables were in it.
Should it be? Do I need to include it in all of the scenes I want it to have access to it?
Read the edit in my first post it says something important about GameObject.Find
But again about the game state manager is actually for more complex games
Answer by Bunny83 · Jul 23, 2013 at 09:42 PM
Make sure that your "gameController" gameobject is actually a root GameObject. If it's a child of another GameObject DontDestroyOnLoad won't do anything since if the parent is destroyed the childs will also be destroyed.
Change your "searching" to this:
GCS = FindObjectOfType(GameController);
You might also add a static variable to your GameController class which holds a reference to the one instance in the scene:
// in your GameController.js
static var instance : GameController;
function Awake()
{
transform.parent = null; // force top-level GameObject just in case
instance = this;
DontDestroyOnLoad(gameObject);
}
Now you can access your GameController from any other script like this:
GameController.instance
Hi Bunny83,
I was able to get something working from an earlier post, but I'll try your suggestion also, as I really would like to have a thorough understanding of how to do this.
I'll respond after I try it this evening.
Thank you for responding.
Hi Bunny83,
Thank you for your response. I just tried your solution and it worked just as you thought it would.
Thank you again.
The first two sentences are gold, thank you. It seems intuitive, but I forgot about this and I suspect others could as well.
Yep ! Great answer ! I was wondering why DontDestroyOnLoad doesn't work. Now it works like a charm !
Your answer
Follow this Question
Related Questions
reloading game from main menu problem 1 Answer
donotdestroy for hud - but hud appears on main menu :( 1 Answer
how to Undo DontDestroyOnLoad 1 Answer
Can you undo "dont destroy on load"? 1 Answer
dontdestroyonload object references 0 Answers