Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Chrras · Apr 18, 2016 at 03:47 PM · c#scripting problemif-statements

Problems with if-statements (C#)

Hello. I am making a game, my first proper game, and I have some problems. The player is an object that can change properties and material when a key is pressed.

It works fine enough, when I just have one double-if statement - even though it doesn't detect every key-hit, sometimes I have to press the key twice (I don't know why). But when I have one double-if statement for each playerstate (true/false), it doesn't work. This is part of my script:

 void FixedUpdate (){
         if (standardPlayer == true) {
             if (Input.GetKeyDown(KeyCode.Q)){
                 standardPlayer = false;
                 Renderer rend = GetComponent<Renderer>(); rend.material = materials[1];
             }
         }
         if (standardPlayer == false) {
             if (Input.GetKeyDown (KeyCode.Q)) {
                 standardPlayer = true;
                 Renderer rend = GetComponent<Renderer> (); rend.material = materials[0];
             }
         }
     }

The full class script can be found here: http://pastebin.com/1KXkKfNb

Any idea how I can fix it, so the player can toggle between the two states when he so desires? Also, is it best to place it in update or fixedUpdate?

Thanks in advance

Comment
Add comment · Show 1
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 Chrras · Apr 18, 2016 at 03:56 PM 0
Share

Also, I tried using and (&&), ins$$anonymous$$d of having a second if-statement inside the first, but that didn't work either.

Just if anyone was wondering about the weird setup, I just tried a lot of things to make it work, but it won't

1 Reply

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

Answer by Cepheid · Apr 18, 2016 at 04:15 PM

Hi there @Chrras

When it comes to checking for input from the user it's best to place it within the Update() method rather than the FixedUpdate() method as FixedUpdate is only called every physics step and so the user key presses will sometimes be missed, hence why you sometimes have to hit the key twice for it to register. Also, FixedUpdate should only be used for physics calculations or mathematical algorithms that require a fixed frame rate.

As for the if statements. When you are comparing something to be either true or false you can make use of the "else" keyword. This keyword will cause the block to of code to be executed if the first conditional statement fails. So for example, consult below:

     void Update ()
     {
         if (standardPlayer == true) 
         {
             if (Input.GetKeyDown(KeyCode.Q))
             {
                 standardPlayer = false;
                 Renderer rend = GetComponent<Renderer>(); rend.material = materials[1];
             }
         }
         else
         {
             if (Input.GetKeyDown (KeyCode.Q)) 
             {
                 standardPlayer = true;
                 Renderer rend = GetComponent<Renderer> (); rend.material = materials[0];
             }
         }
     }

The code above will now check to see if standardPlayer is true. If not, it will then check to see if standardPlayer is false. Now, when you press the q key it will change the material and the standardPlayer variable so that next time, the other condition will become true.

If you were to not do this, then every time you press the Q key, it will evaluate both conditions immediately as it would check the first if statement, and then it would proceed to check the next if statement as well regardless of the outcome from the first, which by this point will have become false due to the first one and thus it looks like the material never changed at all, when it in fact did, it just changed too fast to notice.

I'm sorry if this explanation got lengthy, if you need further information or this didn't solve your problem feel free to let me know. I hope this helps! :)

Comment
Add comment · Show 2 · 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 b1gry4n · Apr 18, 2016 at 04:33 PM 0
Share

Just to simplify the code down, maybe making it easier to read...

     void Update()
     {
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Q))
         {
             if (standardPlayer)
             {
                 standardPlayer = false;
                 Renderer rend = GetComponent<Renderer>(); rend.material = materials[1];
             }
             else {
                 standardPlayer = true;
                 Renderer rend = GetComponent<Renderer>(); rend.material = materials[0];
             }
         }
     }
        
avatar image Chrras · Apr 18, 2016 at 05:08 PM 0
Share

Thanks a lot! That worked perfectly. I should really have asked the question in here before, since I have used several hours, trying to get it to work :)

I really appreciative your help, and your explanation was very understandable :)

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

147 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

If statement not working. 0 Answers

how to instantiate an object from an array above the previously instantiated object from the same array? 1 Answer

I get a "NullReferenceException" when trying to change the text of a UI text box. 1 Answer

C# Script cant find Tagged Object 1 Answer

How to add script to gameObject by script? 2 Answers


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