Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rxanadu · Apr 14, 2013 at 01:29 AM · instantiatedontdestroyonloadfindgameobjectswithtag

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);
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rxanadu · Apr 14, 2013 at 03:41 PM 0
Share

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).

avatar image Lockstep · Apr 15, 2013 at 02:07 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

finding clones and destroying them doesn't work 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges