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.

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*/);


In the end, it should look somewhat like this:

var destroyOnLoad : boolean = false;

function WhateverThisFunctionActuallyIsInYourGame (){
var toDestroy : GameObject = GameObject.Find("prefabPortal");
Application.LoadLevel(/*whatever level you have here in quotes*/);

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() {



I think it could be function Awake() too but I’m not really sure whats the difference :stuck_out_tongue:

Hope it helps :slight_smile: