Destroy object if one already exists upon load
so i have a simple gameobject that i want to DontDestroyOnLoad(gameObject); And check if upon loading a new scene if itself already exists and if so it will destroy itself. how could i go about doing this?
Answer by tormentoarmagedoom · Apr 13, 2018 at 07:37 AM
Good day @Yogurt_ .
I write you the logic, not the code:
Fisrt, you have to create a bool variable, called for example "TheGoodObject", which must be activated in the correct object to preserve
And then, at the Start() or Awake() look for all GameObject like that (i recommend to create a tag, assign it, and use FindObjectsWithTag("Tag") to find them all.
The first time you do it, the array resulting of the search lenght is 1, means there is only one, so you must activate its bool variable.
All next times you make the search, will find your "goodObject" and otherone without the bool actiavted. This way, every time the Start() executes, will find the correctone and do nothing with it (preserve) and destroy the badones.
If helped, Accept the answer and close the Question!
Bye!
this is helpful but not exactly what im trying to achieve i kind of worded my question wrong. ill start with an object in my title scene that plays music, by scene 4 i want to destroy the one that was on the title scene and play a different song, by scene 5 return back to the title scene music how could i go about that?
Your answer
Follow this Question
Related Questions
how can i know if this transform is moving ??? 1 Answer
black screen when you start unity3d 4 Answers
Newbie Simple Melee 1 Answer
roll-a-ball Counter issue? 0 Answers
Assets/sprits/point.cs(24,39): error CS1525: Unexpected symbol `Destroy' 1 Answer