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 zaylan · Jan 17, 2013 at 07:01 PM · gameobjectcollideraudio

one shot Sound on collision. Unity 4.0 problem.

Im trying to do so, if you for example go in to a room i want a sound to start, so I put a cube ,with "is trigger" checked, to the player to collide with and then the gameobject(cube) destroys so it doesnt play a second time when entering the room. I attach the script to the cube and ofcourse sets the players tag to "Player". I have always used this script but now, in 4.0 it doesnt work. I think the gameobject destroys before the sounds begin playing so you cant hear the sound

Script

  var sound : AudioClip;
     
     function OnTriggerEnter( hit : Collider )
         {
     
         if(hit.gameObject.tag == "Player")
             {
             Destroy(gameObject);
             audio.PlayOneShot(sound);
             }
     
         }
Comment
Add comment · Show 8
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 Dave-Carlile · Jan 17, 2013 at 07:03 PM 0
Share

Have you tried swapping the Play and Destroy lines?

 audio.PlayOneShot...
 Destroy(...);
avatar image zaylan · Jan 17, 2013 at 07:38 PM 0
Share

Thx for reply, yes I have and it didnt work.

avatar image Dave-Carlile · Jan 17, 2013 at 08:11 PM 0
Share

Add Debug.Log statements before the if, and inside the if. Also log hit.gameObject.tag and make sure it's set to what you expect. Also, strings are case sensitive - you're checking for "Player", and in your question you mention that you set the tag to "player" (lower case "p").

avatar image zaylan · Jan 17, 2013 at 08:32 PM 0
Share

Im verry new to this so i didnt rly get the Debug.log thing, but the lower case "p" where a mistake sorry.

avatar image Dave-Carlile · Jan 17, 2013 at 08:39 PM 0
Share
 function OnTriggerEnter( hit : Collider )
 {
   Debug.Log("Hit: " + hit.gameObject.tag);

   if (hit.gameObject.tag == "Player")
   {
     Debug.Log("Hit object has Player tag");

     Destroy(gameObject);
     audio.PlayOneShot(sound);
   }
 }

When you run that, look at the console window and it should show a message when OnTriggerEnter gets called, and then another one right before the game object is destroyed. This will let you verify that the code is getting inside your if statement and actually executing the PlayOneShot.

If that works correctly, then I would try commenting out the Destroy and see if the sound plays then.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Dave-Carlile · Jan 17, 2013 at 09:50 PM

You need to destroy the object after a delay. You can do this by calling Invoke and passing in the time. The time should be longer than it takes to play the sound.

So...

 ...
 {
   audio.PlayOneShot(sound);
   Invoke(DestroySelf, 3)
 }


 void DestorySelf()
 {
   Destroy(gameObject);
 }


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 zaylan · Jan 18, 2013 at 03:33 PM 0
Share

if you mean secounds, I only have to put:

Example: yield WaitForSeconds (10);

Between ...PlayOneShot and Destroy... lines

avatar image
0

Answer by $$anonymous$$ · Jan 18, 2013 at 03:53 PM

Destroy() has a time argument, this is the cleanest to do it:

 function OnTriggerEnter( hit : Collider )
 {
    if(hit.gameObject.tag == "Player")
      {
         if (!alreadyPlayed)
         {
            audio.PlayOneShot(sound);
            alreadyPlayed = true;
            Destroy(gameObject, 5);
         }
      }
 }
Comment
Add comment · Show 3 · 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 zaylan · Jan 19, 2013 at 10:59 AM 0
Share

Thx for reply, but it doesnt understand "alreadyPlayed".

avatar image $$anonymous$$ · Jan 19, 2013 at 12:20 PM 0
Share

well maybe you have to declare it...

avatar image zaylan · Jan 19, 2013 at 01:23 PM 0
Share

Like i said before, im still really newb to this :P Could you explain abit please? would be much appreciated.

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

10 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

Related Questions

How to make audio play when passes a collider 1 Answer

Why my sound it's not attaching to my Flashlight 2 Answers

Wheel Collider Rotation Problem 0 Answers

How to emit a sound on specific collision. 1 Answer

Play audio only once in trigger collision entry? 1 Answer


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