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 gonzalitosk8 · Aug 23, 2013 at 02:18 AM · soundwaitforsecondsvehicle

how to apply "WaitForSeconds" ? Help Me

Hello how are you? I need help with a problem .. as applied "WaitForSecond" in this script?

the idea is that at high RPM, sound the exhaust sound, and have to wait some time to return to play .. I let the script, it works .. just repeats each time the sound is just ..

SCRIPT (C#) :

 using UnityEngine;
 using System.Collections;
 
 public class exaustSound : MonoBehaviour {
     
     public AudioClip exaust;
     public float exaustVolume=1f;
     public float exaustpitch=1f;
     
     AudioSource exaustSource;
     [HideInInspector]
     Drivetrain drivetrain;
     
     float volumeFactor;
     int i,k;    
     
     AudioSource CreateAudioSource(AudioClip clip, bool loop, bool playImmediately, Vector3 position) {
         GameObject go = new GameObject("audio");
         go.transform.parent = transform;
         go.transform.localPosition = position;
         go.transform.localRotation = Quaternion.identity;
         go.AddComponent(typeof(AudioSource));
         go.audio.clip = clip;
         go.audio.playOnAwake = false;
         if (loop==true){
             go.audio.volume = 0;
             go.audio.loop = true;
         }
         else 
             go.audio.loop = false;
         
         if (playImmediately) go.audio.Play();
         
         return go.audio;
     }
     
     void Start () {
         
         exaustSource= CreateAudioSource(exaust, false, false,Vector3.zero);
         exaustSource.volume=exaustVolume;
         
     
     }
         
     
     void Update () {
         drivetrain= GetComponent<Drivetrain>();
         if (drivetrain) {        
             if (this.drivetrain.rpm > 10000f) {if (!exaustSource.isPlaying) exaustSource.Play();
             
             
             else exaustSource.Stop();
         }
         
     }
 }
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
4
Best Answer

Answer by clunk47 · Aug 23, 2013 at 06:24 AM

A couple simple ways to use a Coroutine in C#:

 using UnityEngine;
 using System.Collections;
  
 public class Example : MonoBehaviour 
 {
     int count = 0;
     
     void Awake()
     {
         StartCoroutine(MyCoroutine(3));    
     }
     
     IEnumerator MyCoroutine(int WaitTime)
     {
         yield return new WaitForSeconds(WaitTime);
         print ("Next Coroutine");
         StartCoroutine(MyOtherCoroutine());
     }
     
     IEnumerator MyOtherCoroutine()
     {
         while(true)
         {
             yield return new WaitForSeconds(1);
             count++;
             print (count);
             yield return new WaitForEndOfFrame();    
         }
     }
 }


Comment
Add comment · Show 4 · 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 robhuhn · Aug 23, 2013 at 06:31 AM 4
Share

Just in addition to your answer it could be good to know that StopCoroutine("$$anonymous$$yThirdCoroutine"); only works if it was started with StartCoroutine("$$anonymous$$yThirdCoroutine") by passing a string.

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.StopCoroutine.html

avatar image clunk47 · Aug 23, 2013 at 06:44 AM 1
Share

Thanks for the additional input, I'm sure it will be useful to the poster. +1

avatar image gonzalitosk8 · Aug 23, 2013 at 01:46 PM 0
Share

how we apply it in the script to work properly? thanks for reply and help me! :)

avatar image clunk47 · Aug 23, 2013 at 04:19 PM 1
Share

I'm not going to write you a script. This is a working solution. Combined with the comment by @robhuhn, this is plenty of information for you to be able to add this to your script. It's simply using IEnumerator Your$$anonymous$$ethod() ins$$anonymous$$d of void Your$$anonymous$$ethod(). Do some homework from the information you have been provided, and you will get this. It's better to learn from trial and error than it is to just use scripts written by others, you will find it to become easier and easier as you go.

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

18 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

Related Questions

Multiple Cars not working 1 Answer

audio fades 0 Answers

someone is there? 1 Answer

play audio for few seconds on key hit 1 Answer

Vehicle Script Help 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