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 AusAndrew19 · Jun 03, 2013 at 09:12 AM · c#audiodestroyupdate

How to destroy a gameobject by GameObject.Find

I'm using a script that plays audio Through multiple scenes and when i get to the "Game" scene i want the object to destroy itself... I don't know how to acomplish this... But i thought it would be something like this.

Make a new script.. Place it into a blank gameobject in the Game Scene...

 using UnityEngine;
 using System.Collections;
 
 public class StopMusicOnGameScene : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update ()
     {
         GameObject.Find("AudioThroughScene");
         Destroy (gameObject);
         Destroy(this);
     }
 }

But as you can see this wont work because its deleteing the gameobject itself before it deletes the AudioThroughScene Object... How can i acomplish this?

I'm new to Scripting Sorry.

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
3
Best Answer

Answer by Shgoedt · Jun 03, 2013 at 09:26 AM

Hi,

You're currently destroying "this" component. This, however, means the script that it's run through.

try this:

 using UnityEngine;
 using System.Collections;
  
 public class StopMusicOnGameScene : MonoBehaviour {
 
     public GameObject AudioObject;
     
     // Use this for initialization
     void Start () {
         AudioObject = GameObject.Find("AudioThroughScene");
     }
  
     // Update is called once per frame
     void Update ()
     {
        Destroy(AudioObject);
     }
 }

I don't know your code but this should do, for now. And you should realy try to never do a GameObject.Find in the Update. GameObject.Find is a pretty heavy call.

Comment
Add comment · Show 4 · 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 AusAndrew19 · Jun 03, 2013 at 10:07 AM 0
Share

This works. Thanks

avatar image Hamtaro · Feb 05, 2016 at 11:52 PM 0
Share

Can this be used on buttons?

avatar image Xitech_ Hamtaro · May 11, 2016 at 01:58 PM 0
Share

Yes. Why would it not?

avatar image Arctype · May 11, 2016 at 01:15 PM 0
Share

@Shgoedt that was great. Helped me remove a DontdestroyOnLoad script that i only wanted to go through to 1 other scene and not the others. I would of replied under your post but i didnt have permissions apparently!?

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

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

fmod 3d listener only updates once at start of script 0 Answers

Calling sound once in update 1 Answer

c# and game objects active state issues 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 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