Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 MagicN · Oct 30, 2015 at 09:16 PM · collideraudiotrigger

Adding Sound To Collectable Object

I'm trying to play a sound when the tagged "Player" picks up a collectable tagged "Pick up" I tried 4 hours worth of coding and can't figure it out. Any ideas? I got the objects to disappear and be collected in numeral values already, just need to figure out how to get the sound when it disappears/inactive. This is for Unity 5 by the way.

 void OnTriggerEnter(Collider other)
         {
             if (other.gameObject.CompareTag ("Pick Up")) 
             {
                 other.gameObject.SetActive (false);
                 count = count + 1;
                 SetCountText ();
             }
     
         }

^^^ This is the Player coding at which the Object is not Destroyed but Inactive when collided

 #pragma strict
 
 function OnTriggerEnter(otherObj: Collider){
     if (otherObj.tag == "Player"){
         GetComponent.<AudioSource>().Play();
     } 
     else {
         GetComponent.<AudioSource>().Stop();
     }
 }


^^^ This is the coding used on the "Pick Up" item trying to use an Audio Source type Don't know if this is the right coding but I have tried manipulating a DestroyObject script and still cant figure it out. Thanks in advance guys, you all are always so good with replying.

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

Answer by hexagonius · Oct 31, 2015 at 11:50 AM

The problem here is, that the pickup tries to play sound, while at the same time the player disables it, effectively avoiding it to be played. Either some other gameobject is responsible for playing sounds (which is more easy I think), or you'd need an alternative to acutally disabling the whole pickup gameobject. You could try this:

  • remove "other.gameObject.SetActive (false);" from the player

  • change the pickup to this:

        function OnTriggerEnter(otherObj: Collider){
              if (otherObj.tag == "Player"){
                  GetComponent.<AudioSource>().Play();
                  GetComponent.<Collider>().enabled = false;
                  GetComponent.<Renderer>().enabled = false;
              } 
          }
    
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 MagicN · Nov 01, 2015 at 11:25 PM 0
Share

Yes I was thinking about using a "WaitForSeconds" on the Player so it was able to play the sound before it was disabled. But let me try your option and see how it works. Thanks!

avatar image MagicN · Nov 04, 2015 at 08:14 PM 0
Share

Hmm no luck. What about if I add a script to the player when he collides with the prefab/pickup?

avatar image hexagonius MagicN · Nov 04, 2015 at 08:53 PM 0
Share

sure, let him play the sound, why not :) everything is allowed

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Play audio only once in trigger collision entry? 1 Answer

Why isnt OnTriggerEnter Tags working. 1 Answer

Need help making an audio trigger. 2 Answers

Problem triggering using colliders 1 Answer

Can't click gameobject when over another trigger? 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