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 /
  • Help Room /
avatar image
0
Question by Akinaro · Sep 17, 2016 at 05:11 PM · c#coroutinesoundontriggerenter

OnTrigger - playing sound once with delay

Hello! I have small problem with script that should play sound once, and after specific delay enable gravity on object. Here is script:

 using UnityEngine;
 using System.Collections;
 
 public class gravity_trigger : MonoBehaviour {
 
 public AudioClip sound;
 AudioSource audio;
 public Rigidbody ActiveObject; 
 public int TimeDelay = 1;
 public float SoundVolume = 1f;
 public bool AlreadyPlayed = false;
 
 
 void Start() 
     {
     StartCoroutine("OnTriggerEnter");
     audio = GetComponent<AudioSource>();
     }
 
 IEnumerator OnTriggerEnter(Collider otherObj) 
     {
         if (otherObj.tag == "Player")
         {
         audio.PlayOneShot(sound, SoundVolume);
         AlreadyPlayed = true;
         yield return new WaitForSeconds(TimeDelay);
         ActiveObject.useGravity = true;
         ActiveObject.isKinematic = false;
         }
     }
 }

Technically it work: Player enter platform that have trigger zone, Sound is playing, and then after one second platform fall down thanks to enabled gravity.

Problem is that sound play again and again, everytime when Player enter that trigger zone, and also I have this error in console:

Failed to call function OnTriggerEnter of class gravity_trigger Calling function OnTriggerEnter with no parameters but the function requires 1.

So question is how to properly set that script to play that sound only once, and what parameter that function need?

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 TBruce · Sep 17, 2016 at 05:42 PM

For what you are looking to do then at a minimum your coroutine needs to look like this

 IEnumerator OnTriggerEnter(Collider otherObj) 
 {
     // if ActiveObject.useGravity is false, the sound has not been played
     if ((otherObj.tag == "Player") && (ActiveObject.useGravity == false))
     {
         if (!AlreadyPlayed) // this is redundant unless you remove '(ActiveObject.useGravity == false)' above
         {
             audio.PlayOneShot(sound, SoundVolume);
             AlreadyPlayed = true;
         }
         yield return new WaitForSeconds(TimeDelay);
         ActiveObject.useGravity = true;
         ActiveObject.isKinematic = false;
     }
     yield return null; // coroutines must always return a value
 }

But instead of using the OnTriggerEnter() as a coroutine you can do this instead

 void OnTriggerEnter(Collider otherObj) 
 {
     // if ActiveObject.useGravity is false, the sound has not been played
     // no use in calling the coroutine
     if ((otherObj.tag == "Player") && (ActiveObject.useGravity == false))
     {
         StartCoroutine(PlaySoundSetGravityAndWait());
     }
 }
 
 IEnumerator PlaySoundSetGravityAndWait() // too descriptive I know
 {
     if (!AlreadyPlayed) // this is redundant unless you remove '(ActiveObject.useGravity == false)' above
     {
         audio.PlayOneShot(sound, SoundVolume);
         AlreadyPlayed = true;
     }
     yield return new WaitForSeconds(TimeDelay);
     ActiveObject.useGravity = true;
     ActiveObject.isKinematic = false;
 }

Also, if the platform ever goes back up to its original position and you want to play the sound again, you do not need the separate bool value AlreadyPlayed to determine if the sound has been played. Just go by the ActiveObject.useGravity value.

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 TBruce · Sep 17, 2016 at 06:22 PM 1
Share

@Akinaro I am glad that this worked for you. BTW, which way did you go with this?

avatar image Akinaro TBruce · Sep 17, 2016 at 06:29 PM 0
Share

I used second version, because it look like it would be easier for me to modify it for future use. Im still green in coding(my job in every project is graphic, design) but with every code Im trying to write and with help, like your here, I think I should make less and less errors like that ;-)

avatar image TBruce Akinaro · Sep 17, 2016 at 06:52 PM 1
Share

I myself am a programmer. I need to rely of other sources for graphics. In the end we all need others help. Good luck in your endeavors.

avatar image
0

Answer by Akinaro · Sep 17, 2016 at 06:26 PM

Thank you @Mavina ! Both work perfectly.

I still had that error, but then I realize that I left "StartCoroutine" in "void Start()", so now its clean and work perfectly.

Thanks again for your help!

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

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

Related Questions

The order of the sound played 0 Answers

Coroutine on collision 1 Answer

Help to play music whenever the character enters a GameObject... 1 Answer

why OnTriggerEnter is not work if with void Update 2 Answers

I want to run it after running the demo, the demo1 wait three seconds .However, this script is ignored the run . The error is not . 0 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