I have a game object in Frontend that has a script attached that does the following in Start()
a) Loads a level using
Application.LoadLevel(“Level1”)
b) Accesses an attached script and calls a method in that script to instantiate a bunch of brick prefabs
BrickManager = GetComponent(typeof(BrickManager)) as BrickManager;
BrickManager.GenerateBricks(6);
Ok all goes well, except that the brick objects that have been instantiated by the BrickManager run once and then disappear. I do have DontDestroyOnLoad(this) in my Frontend.Wake() method by the way.
I am probably doing something rather noobish, can anyone point me in the right direction?
I’m guessing that its something to do with instantiating stuff just after loading a scene?
It sounds like without seeing your exact code that you’re creating the bricks from the object in the first scene. Any object instantiated without DontDestroyOnLoad in one scene will be destroyed when the second is loaded. You’re saving your management object from being destroyed but not the bricks themselves.
If you’re only going to use the bricks in one scene maybe instantiate them in Level1 instead.
Thanks Timmer thats the direction I needed to go. I was only setting DontDestroyOnLoad for the main game object
The game will have 4 different scenes:
Frontend
Scene 1
Scene 2
Scene 3
The problem we have is that the game has at least 30 levels spread across 3 different scenes so we can’t really have a per scene set-up; which seems to be the standard Unity way.
My idea is to have a main game controller object / script in the Frontend scene that handles the loading of the correct scene for the players current level and then instantiate all of the objects required for that level. is this bad practice, if so what would be the best approach to tackle this sort of problem?
You’re sure you definitely need to do it this way rather than have one scene per level?
If so, you could have an object in the Frontend scene set with DontDestroyOnLoad. The idea of this object is to pass a message between scenes - you could have a string or other variable stating which level you want to set up. Set this just before you call Application.LoadLevel to load the appropriate scene. Then, in the Start method of an object in the new scene, look at the string in the message-passing object to see which level to construct.
I’d recommend this over setting up all your objects in the Frontend scene and then applying DontDestroyOnLoad to them all. The design will be more modular, and you won’t have to destroy all a level’s objects explicitly before returning to the Frontend.
Thanks andeeee. We could set up 30 or so scenes containing all our preset object configurations but I’m worried about:
a) It seems like an awful waste of time and the level design guy will hate me
b) I’m concerned about how much memory this will waste (we’re targetting iPhone initially)
It would certainly be a lot easier for me to do it that way as I’m still learning Unity
If the memory footprint per scene is very small then I may try moving over to your approach and buy the designer a box of tissues
So just to clarify, once an object has had DontDetroyOnLoad called on it no other scene being loaded including the original scene is going to affect it correct?
Calling DontDetroyOnLoad() in a script thats attached to an object will ensure that the object doesnt get destroyed when you load a different scene using LoadLevel()
Scene 1 has an object in it that has a script attached to it. There is some initialization done to that Gameobject etc … Later in the game logic the script calls DontDestroyOnLoad on this GameObject then does a LoadLevel(2).
Later on once scene 2 is done or the user exits the level, scene 2 calls DontDestroyOnLoad on this GameObject then chains back by doing a LoadLevel (1).
How does Unity decide what code it’s going to skip when it LoadLevel 1?
Or is that up to my script logic to figure out somehow?
I should really just test it (and probably will) but since I think you’ve already been there I thought I would ask.