Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

[Solved] count int ?

Discussion in 'Getting Started' started by Quast, Jan 8, 2017.

  1. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    556
    Hi

    How to make a simple count from 0 to 5. ? when i press SPACE. 0 1 2 3 4 5 ?
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Check out a for loop.
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,819
    Or use more words to explain what you mean. Is there a Text on screen that should show the numbers 0 through 5, one at a time, 1 second apart? Or do you have six objects you need to assign a number to? Or one of the dozens of other scenarios that would fit your question?

    The answers to these different scenarios will be quite different.
     
  4. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    556
    counting man. simple count. from 0 to 5. "0,1,2,3,4,5" count in seconds while I am pressing C.
    Code (CSharp):
    1.  
    2. int number = 0;
    3.  
    4.      if(Input.GetKeyDown(KeyCode.C))
    5.         {
    6.              // count number up
    7.             if ( number >=5) {
    8.                 number = 5;
    9.             }
    10.  
    11.             Debug.Log(" counting up " + number);
    12.         }
    13.         else {
    14.             // count number down
    15.  
    16.             if ( number >=0) {
    17.                 number = 0;
    18.             }
    19.             Debug.Log(" counting down " + number);
    20.  
    21.         }
     
    Last edited: Jan 8, 2017
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,819
    OK, I think you didn't understand my point — there are many different ways and things to count. But you've now given enough clues to guess more or less what you mean anyway.

    Here's how I would do it.
    1. Add a value field to your class. IMPORTANT: this must be a float.
    2. In your Update method, use Input.GetKey (not Input.GetKeyDown) to check whether C is pressed.
      1. If it is, add Time.deltaTime to your value.
      2. Else, subtract Time.deltaTime from your value.
    3. Display (somehow — I doubt Debug.Logs are what you really want) the value, truncated to an integer (for example, with Mathf.FloorToInt).
     
    Kiwasi and Quast like this.
  6. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    556
    thank you Joe. its work now. here is my script:
    Code (CSharp):
    1.     public float number = 0;
    2.     void Update() {
    3.  
    4.         if(Input.GetKey(KeyCode.C))
    5.         {
    6.             number += Time.deltaTime;
    7.             if ( number >=5) {
    8.                 number = 5;
    9.             }
    10.  
    11.             Debug.Log(" counting up " + Mathf.FloorToInt(number));
    12.         }
    13.         else {
    14.             number -= Time.deltaTime;
    15.  
    16.             if ( number <=0) {
    17.                 number = 0;
    18.             }
    19.             Debug.Log(" counting down " + Mathf.FloorToInt(number));
    20.  
    21.         }
    22.  
    23.     }
    If there is anything can i add to improve the code please write it.
    Thank you so much.
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,819
    Looks reasonable to me.
     
    Quast likes this.