- Home /
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();
}
}
don't you want to put what's in Awake into Start ins$$anonymous$$d?
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
Answer by Next Beat Games · Mar 31, 2013 at 03:20 AM
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();
}
}
Answer by Unitraxx · Mar 30, 2013 at 11:15 PM
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();
}
}
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.
"i will accept your answer once you read my comments"
if you don't accept his answer someone will just delete your question :-)
Say, you don't ever need to write
if ( xyz == true )
in fact, you can simply write
if ( xyz )
{aside ... in some rare situations, I believe it is clearer if you in fact do write "== true" but as a broad general rule, do not do it.}
for anyone reading who doesn't know, if you want false, it is:
if ( ! xyz )
"if not xyz" in other words, if xyz is false.
Closing out questions gives YOU reputation points.
Generally only YOU can close out a question, and hence keep the board tidy
You are asking a lot of questions and not closing out many.
Comments like this .. "i will accept your answer once you read my comments" can sound rude online. All you had to say was "hey thanks for the help maybe you could look at this also" or something
People here bend over backwards to help and answer questions.
The internet's been here for decades -- everyone knows you're supposed to politely close-out questions you ask on help sites.
I did not ask explicitly to accept my answer, I just noticed that you never do. $$anonymous$$y code did solve the problem you asked for, it just introduced a new "problem". I then added as a comment what was causing this, which was very easy to solve and was already suggested in one of your other 1000 questions.
Asking OP to accept answer is very tricky. If we don't answer their question, OP will say why should we care, we never border to answer their question. If we have answered their question, we will be accused of begging for karma.
OPs, please try to have a certain amount of respect towards to those who answer your questions; these people spend their time and effort to save you from troubles and headaches, you don't have to approve the answer if it doesn't help you, but at the very least try to be less rude and more appreciative on their effort to help you.