Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by inglipX · Mar 30, 2013 at 11:13 PM · ifvartrue

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();
         }
 }
     
 
 
 
     
 
     
 
 
 
 
 
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Mar 31, 2013 at 02:23 AM 2
Share

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

avatar image Next Beat Games · Mar 31, 2013 at 02:53 AM 0
Share

don't you want to put what's in Awake into Start ins$$anonymous$$d?

avatar image Eric5h5 · Mar 31, 2013 at 02:57 AM 2
Share

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

avatar image AlucardJay · Mar 31, 2013 at 04:41 AM 2
Share

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 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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();
        }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
3

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

 }
  
Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Unitraxx · Mar 30, 2013 at 11:27 PM 3
Share

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.

avatar image Fattie · Mar 31, 2013 at 11:46 AM 2
Share

"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.

avatar image Fattie · Mar 31, 2013 at 12:37 PM 2
Share

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.

avatar image Unitraxx · Mar 31, 2013 at 12:47 PM 2
Share

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.

avatar image Chronos-L · Mar 31, 2013 at 01:00 PM 2
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

BCE0023: No appropriate version of 'UnityEngine.Random.Range' for the argument list '(System.Object)' was found. 0 Answers

How are the if's in a GUI.Toolbar handled? 1 Answer

mouse displays GUI text when on objects 3 Answers

How to make a GUI appear and disappear with the same button? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges