- Home /
How to prevent objects instantiating between scenes?
I use DontDestroyOnLoad(this); on one of my object and when going back and forth between scenes, it spawns multiple same objects because they never destroy. I want that there would be only one object. Is there some particular code to prevent object from spawning at the start of certain scene?
Answer by Wuzseen · Jun 26, 2013 at 02:54 PM
This is a problem with several solutions.
The easiest and most intuitive is to instantiate your dontdestroyonload objects in a scene before the one where they are actively used. That is, you'll have a "loading" scene. Then you add some logic on your dontdestroy objects that will destroy themselves when they need to. You can add a flag to an object as well to do similar things. The important thing that makes this work is that you don't go to the loading scene a second time.
This works and will most often get the job done for things like audio. But sometimes you have to go deeper depending on the situation.
The next most common way I tackle this issue is to have an object in my scene that handles instantiating my dont destroy on loaded objects by checking if they already exist or not.
if(GameObject.Find("levelObj(Clone)") == null) {
Instantiate (levelObj);
}
I use the above code to do this.
levelObj is an object that has dontdestroyonload in it.
This script resides on a separate object that isn't destroyed on load, "levelObj" is a public variable for this code that I define via the inspector.
This script checks if the dontdestroy object already exists. If it already does, don't create a new one.
Damnit you are genious. I was thinking about to make a zero level but didn't want to because of laziness and I thought that would mess everything up. But I'll try the 2nd method, that should work I believe.
PS Now I know why there are those "press strart to begin" -screens lol.
I had that same realization a month or two after starting using Unity. It's not really the same idea for every game, but for how unity is used it is a natural use for that sort of screen.
$$anonymous$$eep in $$anonymous$$d you can just have an empty scene where this happens, it will load the next scene really fast--before you even know it, really.
Don't forget the second solution I posted. It's far more useful as you don't have to swap between scenes while editing which can put strain on your workflow.
Thank you @Wuzeen.
I used your first strategy, creating another scene for loading. I deleted everything in the scene and added an empty gameobject with this script. Objects is an array where you can add any object that should be instantiated. Then, a for loop runs and instantiates them and says not to destroy them. After that, the first scene is loaded, which you can set with the startScene variable.
#pragma strict
var objects : GameObject[];
var startScene : String;
function Start () {
for(var i : int = 0; i < objects.length; i++){
Instantiate(objects[i]);
DontDestroyOnLoad(objects[i]);
}
Application.LoadLevel(startScene);
}
Answer by AVividLight · Jun 26, 2013 at 02:55 PM
Hey Heke,
I believe the problem you're having is that somewhere in your code you instantiate the object, without checking if it already exists. There are many ways you could fix this, and using a variable would probably be easiest. You could also give your object a tag, and check if the tag exists before instantiating.
I hope I've helped. - Gibson
Answer by Joe-Censored · Mar 17, 2017 at 08:36 PM
I give the object a unique tag, then on scene load do a FindObjectWithTag, if no object is found I instantiate the object, otherwise I don't.
Your answer
Follow this Question
Related Questions
How do I keep objects destroyed between scenes? 1 Answer
Destroy all objects in the scene? 0 Answers
Destroying object using his name and raycast 2 Answers
Destroy object on button push? 2 Answers
Recover Object After Destroyed 0 Answers