• 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 ajnolasco11 · Dec 15, 2013 at 03:18 PM · gameobjectdestroyduplicatedestroygameobject

How to destroy an object if it already exist in a scene

Hello I'm new here I'm having a hard time with the Audio(BG Music) of my scene, I created a BG Music to my first scene that will continuously play even going to another scene. My problem is that if I go back to the that scene(first scene) where I put my BG Music it will play the BG Music again, making it double BG Music(x3,x4,x5.... As I go back to that scene again)

Sorry for my wrong grammar :)

BTW this is my code:

 function Awake()
 {
     if(GameObject.Find("TronBG") == 1)
     {
         Debug.Log("IF");
         Destroy(gameObject);
     }
     else
     {
         Debug.Log("ELSE");
         DontDestroyOnLoad(gameObject);
         SoundSource = gameObject.AddComponent(AudioSource);
         SoundSource.playOnAwake = false;
         SoundSource.rolloffMode = AudioRolloffMode.Logarithmic;
         SoundSource.loop = true;
             
     }
     
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by KellyThomas · Dec 15, 2013 at 03:28 PM

If you give them a unique tag this will work:

 function Awake() {
     if (GameObject.FindGameObjectsWithTag(gameObject.tag).length > 1){
         Destroy(gameObject);
     }
     else {
         //initialize
     }
 }

The second (or more) instance will find more than one object with their tag.

Comment
Add comment · Show 1 · 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 ajnolasco11 · Dec 15, 2013 at 03:50 PM 0
Share

WOW! thanks a lot bro! :D Finaly I can pass my project in GameDev. :D $$anonymous$$y next problem is to convert it into an android game :D

avatar image
2

Answer by aldonaletto · Dec 15, 2013 at 03:52 PM

Use a static variable to reference the first TronBG object, and suicide any TronBG object at Awake when this variable has been set:

 static var bgAudio: GameObject;

 function Awake(){
   if (bgAudio){ // if bgAudio already references some object...
     Destroy(gameObject); // suicide
   } else { // if bgAudio is null, this is the first TronBG object:
     bgAudio = gameObject; // assign this one
     SoundSource = gameObject.AddComponent(AudioSource);
     SoundSource.playOnAwake = false;
     SoundSource.rolloffMode = AudioRolloffMode.Logarithmic;
     SoundSource.loop = true;
   }
 }


 
Comment
Add comment · Show 1 · 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 KellyThomas · Dec 15, 2013 at 10:52 PM 0
Share

I like this: less noise placed in the tag system.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Destroying the object that I am selecting 1 Answer

Destroy by Tag isnt working,Can't Destroy By Specific Tags 2 Answers

How to assign a color to a game object & destroy it when it collides with another game object of same Color 1 Answer

Optimize huge number of Destroy() calls (cannot use object pooling) 3 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