x


undoing DontDestroyOnLoad without immediately destroying the object

After calling DontDestroyOnLoad on a game object that we want to pass between levels. Eventually we want to finally destroy it... say to reset the game to the initial state.

Is there a way of undoing DontDestroyOnLoad without immediately destroying the object..

the command: Destroy(this.gameObject); will cause issues if other game objects that are in the same scene still depend on it. I just want it to get destroyed with all of the other game objects on the next level load.

more ▼

asked May 26 '10 at 10:22 PM

badneighbor gravatar image

badneighbor
99 5 5 12

I to wish you could write something like DontDestroyOnLoad (go, true); The boolean would say wether it is destroyed or not on load.

May 26 '10 at 10:26 PM Daniel 6
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

No way to do it automagically no

best way i can think of doing it is this:

var destroyOnLoad = false;

function OnLevelWasLoaded(level : int)
{
    if (destroyOnLoad) Destroy(gameObject);
}

that way you get your object destroyed before the first update in the new level

more ▼

answered May 26 '10 at 11:26 PM

Mike 3 gravatar image

Mike 3
30.5k 10 65 252

Due to event execution ordering the above code was giving me runtime threading errors.

I think the issue is that Destroy does not take action immediately and there is some overlap between its call, other event phases of the event loop, and its action.

The run-time error goes away if I do replace Destroy with DestroyImmediate. (however there could be still un-reported errors)... thats it for now.

May 27 '10 at 05:51 PM badneighbor
(comments are locked)
10|3000 characters needed characters left

If you're destroying it and it's then being recreated for the level you've just loaded to (I'm assuming this from your comment about destroy and destroyimmediate) why don't you make a reset method for the object that puts it back in its initial state.

That way you could just replace the destruction with a reset and carry on.

more ▼

answered Jun 27 '10 at 12:06 PM

Eagle32 gravatar image

Eagle32
452 2 6 17

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x263
x82

asked: May 26 '10 at 10:22 PM

Seen: 2449 times

Last Updated: May 26 '10 at 10:22 PM