I am trying to play a sound when isMoving is true (in my function Awake)
No errors but the sound doesn’t play when it is true and it shows true in inspector too.
var movingSound : AudioClip;
var idleSound : AudioClip;
var isIdle : boolean;
var isMoving : boolean;
function Update ()
{
if (Input.GetKey("w") || Input.GetKey("s")
|| Input.GetKey("a") || Input.GetKey("d"))
{
isMoving = true;
isIdle = false;
}
else
{
isMoving = false;
isIdle = true;
}
}
function Awake ()
{
if (isMoving == true)
{
audio.clip = movingSound;
audio.Play();
}
}
2 Answers
2
What is your goal ?
It should work if you place everything from inside the Awake() function, together in the Update() function. (After the already present code.)
var movingSound : AudioClip;
var idleSound : AudioClip;
var isIdle : boolean;
var isMoving : boolean;
function Update (){
if (Input.GetKey("w") || Input.GetKey("s")
|| Input.GetKey("a") || Input.GetKey("d")){
isMoving = true;
isIdle = false;
}else{
isMoving = false;
isIdle = true;
}
if (isMoving == true){
audio.clip = movingSound;
audio.Play();
}
}
If I’m understanding correctly, just use unitraxx’s code with a check if the audiosource is playing or not
var movingSound : AudioClip;
var idleSound : AudioClip;
var isIdle : boolean;
var isMoving : boolean;
function Update (){
if (Input.GetKey("w") || Input.GetKey("s")
|| Input.GetKey("a") || Input.GetKey("d")){
isMoving = true;
isIdle = false;
}else{
isMoving = false;
isIdle = true;
}
if (isMoving && !audio.isPlaying){
audio.clip = movingSound;
audio.Play();
}
}
Awake is only called once, when the script first starts.
– Eric5h5don't you want to put what's in Awake into Start instead?
– Next_Beat_GamesThat would make no difference at all; Start is also only called once, when the script first starts.
– Eric5h5These are all duplicate questions : * http://answers.unity3d.com/questions/428154/why-am-i-getting-this-error-2.html * http://answers.unity3d.com/questions/428203/if-var-true-do-something-not-being-detected.html * http://answers.unity3d.com/questions/428237/audio-says-it-is-playing-in-audiosource-but-is-not.html @inglipX : Do Not Post Duplicate Questions Watch : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers
– AlucardJay