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 meechan007 · Nov 07, 2015 at 03:35 PM · collisioncoroutinesoundwaitforseconds

Coroutine on collision

I am trying to play a sound which is attached to a mixer when something is hit by a ball. The code works without the coroutine and no destroy gameobject in the code, but the brick needs to delete when hit and i think my code is the best way to do it (although i am probably wrong). But the problem is that i can't get the coroutine to start on a collision with an object. I removed the collision part of the code i was trying in order to allow the code to work but now all the objects this is attached too delete after 1 second.

How can i start a coroutine on a collision or if that is not possible what is a work around.

Thanks for any and all help.

using UnityEngine; using System.Collections;

public class Bricks : MonoBehaviour {

 public GameObject brickParticle;
 public AudioClip Brick_breaking;
 private AudioSource SoundFx;

 void Awake()
 {
     SoundFx = GetComponent<AudioSource>();
     StartCoroutine(Example());
 }

 IEnumerator Example()
 {
     SoundFx.PlayOneShot(Brick_breaking, 1F);
     Instantiate(brickParticle, transform.position, Quaternion.identity);
     GM.instance.DestroyBrick();
     yield return new WaitForSeconds[1];



 }

}

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 Statement · Nov 07, 2015 at 10:51 PM 0
Share

If you destroy the object which has the audio source, the audio source will stop playing.

1 Reply

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

Answer by Cepheid · Nov 07, 2015 at 10:02 PM

Hi there @meechan007 To start a coroutine within a collision all you should need to put is the following:

     public GameObject brickParticle;
     public AudioClip Brick_breaking;
     private AudioSource SoundFx;
 
     void Awake()
     {
         SoundFx = GetComponent<AudioSource>();
     }
 
     void OnCollisionEnter (Collision other)
     {
         StartCoroutine("Example");
     }
 
     IEnumerator Example()
     {
         SoundFx.PlayOneShot(Brick_breaking, 1F);
         Instantiate(brickParticle, transform.position, Quaternion.identity);
         GM.instance.DestroyBrick();
         yield return new WaitForSeconds(1);
     }

If think the issue you may be having might be due to you using two [ ] angle brackets for your WaitForSeconds statement rather than two ( ) parenthesis. I hope it helps! If not please feel free to tell me and I'll try to help further. :)

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

40 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

Related Questions

Missile Boucne Issues (need a creative solution) 0 Answers

Object lerping forwards a set distance every 3 seconds. 1 Answer

How to reduce lag in coroutines - specifically WaitForSeconds()? 1 Answer

WaitForSeconds returns control to calling routine before it completes? 0 Answers

Could not load source 'Coroutines.cs': No source available. 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