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 RuiFerreira · Feb 24, 2014 at 01:55 AM · collisionerrorplay

Collision destroy help.

Hey I have two gameObjects that's going to collide to eachOther I want to how to make one destroy the other on collision. Here is what I got. I'm new to coding. Also there is a error "Play".

 var Hit : GameObject;
 var sound : AudioClip;
 
 function OnCollisionEnter (collision : Collision)
 {
     if (collision.gameObject.tag == "Destroy");
     Destroy (gameObject);
     Play.AudioClip;
 
 
 }
Comment
Add comment · Show 1
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 highpockets · Feb 24, 2014 at 02:15 AM 0
Share

As it stands right now. Whatever object this script is on will be destroyed if it collides with a game object that is tagged "Destroy"...

1 Reply

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

Answer by JeffreyD · Feb 24, 2014 at 05:28 AM

highpockets is correct, so there are a few things to consider. Look at the following modes to your code. I also added some debug statements so you could see what is going on.

If this script is attached to a gameObject and I tag my playerController as "Destroy" then I can run the player into the object to activate the collision. The Short sound will play and then the object is destroyed after a short delay in order to allow time for the AudioClip to play. If you want the Object to be destroyed immediately then you will not get any sound because the AudioSource is Destroyed, which is attached to the object just destroyed. So, you'll need to think about putting the sound intelligence (and perhaps the destroy intelligence as well) into the "Destroy" Object. I'm not sure what you're trying to do so it's up to you.

Also see the links to AudioSource Info in the ScriptRef Doc. http://docs.unity3d.com/Documentation/ScriptReference/AudioSource-clip.html

Let me know if my rambling makes sense.

 var Hit : GameObject;  // this in never used???
 var sound : AudioClip;
 
 function OnCollisionEnter(col : Collision)
 {
    Debug.Log("ENTER OnCOllisionEnter Collion.gameObject.name ==> " + col.gameObject.name + "  tag ==> " + col.gameObject.tag);
    
     if (col.gameObject.tag == "Destroy")
     {
             Debug.Log("Destroy gameObject.name ==> " + gameObject.name);
             Destroy (gameObject, 1);  // destroy object in 1 second to give time for the clip to play.
     }
                        
        // Assign the audio clip and play it
        // Need to have an audioSource on this object in order for clip to play
        // NOTE: If you destory the object the AudioSource is also e destroyed so clip will not play.
        //       So, give time in the above destory call to allow clip to play.
     Debug.Log(" AudioClip is ==> " + sound); 
     audio.clip = sound;
     audio.Play();
   
     Debug.Log("Exit OnCollisionEnter");
 }

Comment
Add comment · 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

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

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

Related Questions

Multiple Cars not working 1 Answer

Showing a picture on collision errors 1 Answer

Block Collision Sliding off 0 Answers

Tags not working? 1 Answer

I got an error that will not go away and doesn't seem to have a solution. 2 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