- Home /
Destroy vs Don't Destroy
My gameObject has a script on it. The script prevents my gameObject from being destroyed when a new level is loaded.
However, I have one specific level where I'd like the gameObject to be destroyed upon loading the level.
How can I force the gameObject to destroy itself, when I'm also forcing the same gameObject NOT to destroy itself?
Please help if you can. Thank you.
Answer by You! · Jun 11, 2012 at 11:43 PM
Don't you love conditional statements?
What you need to do is put a little extra code in the script you have the function "Application.LoadLevel()" Firstly, you need a boolean at the beginning of the script (like @multinfs has). Then, you need this after the Application.LoadLevel() or just before that function:
var toDestroy : GameObject = GameObject.Find(/*name of object here*/);
if(destroyOnLoad){
Destroy(toDestroy.gameObject);
}
In the end, it should look somewhat like this:
var destroyOnLoad : boolean = false;
function WhateverThisFunctionActuallyIsInYourGame (){
var toDestroy : GameObject = GameObject.Find("prefabPortal");
if(destroyOnLoad){
Destroy(toDestroygameObject);
}
Application.LoadLevel(/*whatever level you have here in quotes*/);
}
thank you for helping me. I tried to combine both scripts but I failed when I inserted the name of my object. It says "$$anonymous$$ identifier" when I insert the word Portal.
var destroyOnLoad : boolean = false; var toDestroy : GameObject = GameObject.Find(prefabPortal);
function Start() {
if(destroyOnLoad) Destroy(gameObject);
}
if(destroyOnLoad){ Destroy(toDestroy.gameObject); }
You've heard about strings before, right? An object name is not a variable in the script. The name itself should be a string literal which is defined by putting your text between quotation marks"".
GameObject.Find("prefabPortal");
Firstly, don't actually put the if statement outside of a function. Secondly, do not put the code inside of the start function. Wherever you switch the levels is where you need to put the code. It should look somewhat like this:
var destroyOnLoad : boolean = false;
function WhateverThisFunctionActuallyIsInYourGame (){
var toDestroy : GameObject = GameObject.Find("prefabPortal");
if(destroyOnLoad){
Destroy(toDestroygameObject);
}
Application.LoadLevel(/*whatever level you have here in quotes*/);
}
And don't forget to do what @Bunny83 said.
I'll put this code in the answer.
Worked perfectly. Thank you so much for explaining everything. I am still trying to learn how to program so this is very helpful. I appreciate it!
Answer by multinfs · Jun 11, 2012 at 09:50 PM
Well, if I get it right you want a object to be toggleable to be destroyed or not when a level is loaded, please correct me if I'm wrong. Now this could be made in a few ways but I think this would be a easy way:
First you create a prefab if you havent already of the object/objects, and then u just add a script with this:
var destroyOnLoad : boolean = false;
function Start() {
if(destroyOnLoad)
Destroy(gameObject);
}
I think it could be function Awake() too but I'm not really sure whats the difference :P
Hope it helps :)
thank you for the script. Unfortunately it's still not going away. I'm thinking I'll make an empty level between my real levels and destroy all objects when they enter the scene and then send it to the next level, if that makes sense.
thanks again!
Your answer
Follow this Question
Related Questions
Application.LoadLevel not loading levels 1 Answer
Destroying game object next to player 1 Answer
Can I put a Level as a property of a GameObject? 1 Answer
Post-Build Custom Level Loading 2 Answers
level loader script wont work 1 Answer