if (var == true) do something... not being detected

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();
		}
}

Awake is only called once, when the script first starts.

don't you want to put what's in Awake into Start instead?

That would make no difference at all; Start is also only called once, when the script first starts.

These 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

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();
       }

}

ehh, the audio sometime plays in your script and when i let go of W ,A,S or D the audio still is in my audio source EDIT Now when i let go of W A S D the audio then plays

You never seem to accept answers on the questions you ask, consider going back to your older questions and check the checkmark next to the best answer.

change isMoving == true to false plays audio on WASD weird...

i will accept your answer once you read my comments

I read your comment : it's because now the audio clip gets played every frame and only when you stop holding the key, the audio clip actually finishes. As for the check mark : if you accept more answers, people will answer your questions more quickly.

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();
       }
}