- Home /
Trouble with deleting duplicate MusicManager
I'm trying to create an object that plays background music and will persist throughout multiple scenes. Unfortunately, when I loop back to the scene that initially had the object inside, I now have two of the same object.
I've tried to remedy this issue by tracking down and deleting all instances of that object while creating a new object with the same properties as the original. However, when I do this in my script, it gets hit with a "StackOverflowException."
Here's the part of the script I'm having trouble with:
if (GameObject.FindGameObjectsWithTag("MusicManager") == null)
{
Instantiate(musicController, Vector3.zero, Quaternion.identity);
DontDestroyOnLoad(this.gameObject);
}
if (GameObject.FindGameObjectsWithTag("MusicManager").Length > 1)
{
Destroy(gameObject);
}
Answer by Lockstep · Apr 14, 2013 at 02:07 AM
The best way of doing this is making the musik manager a singleton. Code in C#
public class MusicManager : MonoBehaviour{
private static MusicManager instance = null;
void Start(){
if(instance == null){
instance = this;
DontDestroyOnLoad(this.gameObject);
} else {
Destroy(this.gameObject);
}
}
}
This will make sure, that there is never more than one instance of the MusicManager in your scene ever.
It worked, but just to be sure I know what's going on:
I am creating an instance of this script to refer to whenever the game is playing. This instance is static to insure it does not change when the game is playing. If no other instance of this script exists, refer to this object and do not destroy the object this script is attached to; otherwise, destroy this object, as another instance exists.
Since singletons are used to instantiate to one object, it's useful (possible) to keep a single GameObject active throughout the game's lifetime without creating duplicates (e.g. objects like a GameController or SoundController, which manage all of the elements of the game or sound in the game, respectively).
This instance is static to insure it does not change when the game is playing.
This is not really what static means. A variable which is marked as static belongs to the script itself while other variables belong to the instance of the script. Lets say we have an enemy script with a health variable. If you say float health
then every enemy has his own health variable. If you would, on the other hand, say static float health
then the health variable would be unique and every enemy would always have the same health.
While the case with the health is pretty stupid, in the case of the $$anonymous$$usic$$anonymous$$anager we use this behaviour to avoid additional copies of the script. If it is the first instance of the script, then the instance variable is nullwich means we set it to the current instance of the script (`this` keyword). Then, if a second instance is created the instance variable will not be null but refer to the existing instance of the script which will result in the destruction of the new instance.
Note that a static property can also be easily accessed by other scripts (if it is public of course) by using $$anonymous$$yClass.myStaticVariable or $$anonymous$$yClass.$$anonymous$$yStaticFunktion(). $$anonymous$$eep this in $$anonymous$$d. You might need it if you write your own classes, which you will at some point. You are even using this already without knowing. An example for a static variable you might use would be $$anonymous$$athf.PI or for a function you can take Vector3.Cross or many more.
I'd recomend this site as further reference for modifiers or C# scripting in general.
Your answer
Follow this Question
Related Questions
Instantiate new object from scratch 1 Answer
Checking if object intersects? 1 Answer
While loop freezing Unity 3 Answers
Instantiate from persistent GameObject 4 Answers