- Home /
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();
}
}
}
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();
}
}
}
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
Thanks for the additional input, I'm sure it will be useful to the poster. +1
how we apply it in the script to work properly? thanks for reply and help me! :)
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
Follow this Question
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