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 etnom22000 · Nov 23, 2020 at 02:33 AM · scripting problemscripting beginnerbeginnerscriptingbasicsscriptableobject

looking for a way to set an integer to another number based on a condition.

Hello everyone! I am very new to C# and am having some trouble. I use mouse clicks to increase an integer from 1 to 3.

Goal: I want to set the integer counter to 1 when I click the mouse button and the current integer is set to three.

So the sequence starts on 1, i click and it becomes 2, I click again and it becomes 3 and I click once more to set it back to 1.

the problem: I try to do this with if(Input.GetMouseButtonDown(0) && Integer == 3) { Integer = 1 }

When doing this, it skips 3. So i press the mouse on two and it resets to 1 without going to three. Not sure what I am missing with this. Any help would be appreciated.

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 thadstedman · Nov 23, 2020 at 04:24 AM 0
Share

If you want it displayed as UI text put another comment and I can show you how.

2 Replies

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

Answer by thadstedman · Nov 23, 2020 at 04:17 AM

try public int myInt; void Update() { if (myInt == 3) { if (Input.GetKeyDown(KeyCode.Space)) { myInt = 1; } }else { if (Input.GetKeyDown(KeyCode.Space)) { myInt ++; } } Debug.Log(myInt); }

Comment
Add comment · Show 6 · 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 thadstedman · Nov 23, 2020 at 04:17 AM 0
Share

did it work?

avatar image thadstedman · Nov 23, 2020 at 04:19 AM 0
Share

Here is the same code but with better format public int myInt;

     void Update()
     {
         if (myInt == 3)
         {
             if (Input.GetKeyDown(KeyCode.Space))
             {
                 myInt = 1;
             }
         }else
         {
             if (Input.GetKeyDown(KeyCode.Space))
             {
                 myInt ++;
             }
         }
         Debug.Log(myInt);
     }
avatar image thadstedman · Nov 23, 2020 at 04:20 AM 0
Share

Oops I mean

     public int myInt;
 
     void Update()
     {
         if (myInt == 3)
         {
             if (Input.GetKeyDown(KeyCode.Space))
             {
                 myInt = 1;
             }
         }else
         {
             if (Input.GetKeyDown(KeyCode.Space))
             {
                 myInt ++;
             }
         }
         Debug.Log(myInt);
     }
avatar image thadstedman · Nov 23, 2020 at 04:21 AM 0
Share

Oh yeah and change the Debug.Log(myInt); to whatever form you want of showing your int

avatar image thadstedman · Nov 23, 2020 at 04:26 AM 0
Share

If you want it displayed as UI text put another comment and I can show you how.

Show more comments
avatar image
0

Answer by unity_ek98vnTRplGj8Q · Nov 23, 2020 at 06:06 PM

I'm assuming that you have something similar to this

 if(Input.GetMouseButtonDown(0) && Integer == 2) { Integer = 3 }
 if(Input.GetMouseButtonDown(0) && Integer == 3) { Integer = 1 }

In which case your issue would be that when the number changes from 2 to 3, it then immediately checks the next line where GetMouseButtonDown is still true AND your number is now 3, so it immediately changes from 3 to 1. You can fix this by changing your second if to an else if so that it doesn't run on the same frame.

     if(Input.GetMouseButtonDown(0) && Integer == 2) { Integer = 3 }
     else if(Input.GetMouseButtonDown(0) && Integer == 3) { Integer = 1 }


Alternatively, you could always do something like this instead

 maxNum = 3;
 
 if(Input.GetMouseButtonDown(0)){
     Integer++;
     if(Integer > maxNum) Integer = 1;
 }

Which lets you easily change you max number without having to write more code

Comment
Add comment · Show 1 · 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 thadstedman · Nov 30, 2020 at 04:14 AM 0
Share

Good job much more compact than what I put

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

277 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 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

How to implement tokenization of game characters? 1 Answer

Beginner Question: How to get normals from a physics raycast using visual scripting? 0 Answers

Question regarding Raycast hitting UI button object instead of gameobject 0 Answers

How do I make a photography function? 0 Answers

weighted inventory system 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