Search Unity

I have some confusion regarding loops in C#

Discussion in 'Scripting' started by lilwolf4evr, Apr 9, 2021.

  1. lilwolf4evr

    lilwolf4evr

    Joined:
    Oct 8, 2020
    Posts:
    5
    Hey guys, I'm new to the Unity forums and this is my first thread and I have searched before but I didn't find any answer related to confusion I'm having. So my issue is how can I use loops in unity..

    for (int i = 0; i < some value; i++)
    {
    code goes here;
    }

    this is a basic structure for using "for" loop can we use it this way : -


    Code (CSharp):
    1. public class LoopsTesting : MonoBehaviour
    2. {
    3.     public int speed;           //speed currently 0
    4.     public int maxSpeed;        // Random in 60-120
    5.  
    6.     void Start()
    7.     {
    8.         // maxSpeed getting random
    9.             maxSpeed = Random.Range(60,120);
    10.  
    11.         // calling coroutine
    12.             StartCoroutine(SpeedTest());
    13.     }
    14.  
    15.     // every 0.5 seconds speed incrementing by 5
    16.     IEnumerator SpeedTest()
    17.     {
    18.        for (speed = 0; speed < maxSpeed; speed += 5)
    19.         {
    20.             yield return new WaitForSeconds(0.5f);
    21.             Debug.Log(speed);          
    22.         }
    23.              
    24.     }
    25. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Was there an actual question you had or...?
     
    lilwolf4evr and Joe-Censored like this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Usually you define the variable you are manipulating in the for loop. You failed to mention if your code works or not.
     
    lilwolf4evr likes this.
  4. lilwolf4evr

    lilwolf4evr

    Joined:
    Oct 8, 2020
    Posts:
    5
    I
    There is a question clearly I'm asking "can we use for loop in this way or we have to always use for (int i = 0; i <100; i++) or we can use already declared variables inside for loop instead of "i" "
     
  5. lilwolf4evr

    lilwolf4evr

    Joined:
    Oct 8, 2020
    Posts:
    5
    Code worked but is this a correct way of doing this using global variables inside for loop parameters instead of "i" keyword?
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    You can use whatever variable you want.

    In fact, there is not even a rule that the code you put inside the first and last section of the for loop have anything at all to do with the loop itself. You can put literally any statement you want there. For example this is valid code:

    Code (CSharp):
    1.             int banana = 0;
    2.             for (Debug.Log("Hey"); banana < 6; Debug.Log("How ya doin?")) {
    3.                 Debug.Log(banana);
    4.                 banana++;
    5.             }
    If your code works, it is correct. There is no language police that is going to come and stop you.
     
    lilwolf4evr and Joe-Censored like this.
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    "i" isn't a keyword, it is the name of a variable. Often for loops people just use single letter variable names because they are throw away variables.

    Generally in game dev, if the code works and meets your performance and any other requirements you have of it, then it is correct. :)
     
    lilwolf4evr likes this.
  8. lilwolf4evr

    lilwolf4evr

    Joined:
    Oct 8, 2020
    Posts:
    5
    Thank you so much for your answer I'm new and I'm sorry if Question didn't make sense but i understood it now
     
  9. lilwolf4evr

    lilwolf4evr

    Joined:
    Oct 8, 2020
    Posts:
    5
    T
    Thank you so much for the quick response and yes sorry about keyword it is actually a variable :)
     
    Joe-Censored likes this.