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 Pure Phase · May 02, 2010 at 08:22 PM · javascriptaudiopickup

Destroying Collected Objects / Playing Audio

Hi guys,

I've got some pickups in my game that you collect by colliding with them. They are then destroyed and a number is added to your objects counter. Here is (some of) the code I have written:

else if(hit.gameObject.tag == "Helmet1") {

     audio.PlayOneShot(pickup);

     Destroy(hit.gameObject);
     OBJECTNUMBER += 1;
     g_Helmet.animation.Play("g_Inv_HelmetAnimation2");
     print("you now have" + OBJECTNUMBER);

 }

}

Now. This works fine, but what happens is because the audio source is set to the object, and then the object gets destroyed, the sound doesn't play. So to combat this, I put in a yield here:

else if(hit.gameObject.tag == "Helmet1") {

     audio.PlayOneShot(pickup);

yield(1); Destroy(hit.gameObject); OBJECTNUMBER += 1; g_Helmet.animation.Play("g_Inv_HelmetAnimation2"); print("you now have" + OBJECTNUMBER);

 }

}

This enabled the sound to play before the object was destroyed - but - now sometimes the OBJECTNUMBER goes up by two or three, because I am still colliding with the object.

How can I write the code so that it only goes up by 1, and the pickup sound can play?

Thanks!

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

Answer by ThumbStorm · May 03, 2010 at 12:25 AM

You could add the sound to the camera or any game object that wouldn't be destroyed. Just add this to the top of the script:

var soundObj : GameObject;

then use soundObj.audio.PlayOneShot(pickup);

with this you can use the first script and get rid of yield.

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 Pure Phase · May 03, 2010 at 12:46 PM 0
Share

Thanks this works, however I get a weird click sound the first time the audio plays, unless I turn play on awake on. Do you know of any reason why this would occur?

Thanks

avatar image
0

Answer by aburningflame · Jan 25, 2014 at 09:34 PM

  • I dont get a click sound with Play On Awake on.

C# Solution: GameObject>Create Empty>Name it something (I named it Engine) Select the Gameobject in the Hierarchy Component>Add Component>Audio>Audio Source

Enemy.cs

 private GameObject engine;
 
 void Start () {
      direction=Direction.UP;
      engine=GameObject.Find ("Engine");
 }
         
         
 public void TakeDamage(int amt){
      hp-=amt;
      if(hp<=0){
           engine.audio.PlayOneShot (deathSound);
           Destroy(gameObject);
      }
 }
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

1 Person is following this question.

avatar image

Related Questions

There is no 'audioSource' attached to the game object error 1 Answer

Play music troughout whole game in js 1 Answer

Audio Clip constantly play 1 Answer

Audio is repeating. 3 Answers

On Mouse Click Play Sound 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