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);
}