Audio says it is playing in audiosource but is not...

I have it so when i am idle, the idle sound plays and when im moving, moving sound plays, and my audio source does indeed switch sounds but doesn’t play them…Help…

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();
       }else{
       		  audio.clip = idleSound;
       		  audio.Play();
     
       }
 
}

do you have audio listeners? also try this audio.Play(idleSOund); audio.Play(othersound);

2 Answers

2

You are calling the Play function every frame - which resets the sound to the starting position.

var movingSound : AudioClip;
var idleSound : AudioClip;
var isIdle : boolean;
var isMoving : boolean;
var playingIdle : 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){
              if(playingIdle)
              {
                audio.clip = movingSound;
                audio.Play();
                playingIdle = false;
              }
       }else{
              if(!playingIdle)
              {
                audio.clip = idleSound;
                audio.Play();
                playingIdle = true;
             }
 
       }
 
}

thank you IMMENSELY! I have been working on this for a while and now it works!!! Thanks man

You're welcome :) I have to say it's one of those things it's really easy not to think about...

i figured it out thanks

Ah sorry, didn't notice this! Glad you've got it working...

For the people having different problem which causes no sound play

Go to Edit > Project Settings > Audio > (From Inspector) Uncheck Disable Unity Audio if Checked