Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
Incrementation in loop or if statement

So here's the thing. I have a game in which I have a spawner - to spawn obstacles. And I want after every 5th obstacle to increase the speed. Example: 1st, 2nd, 3rd, 4th, 5th obstacles have default speed = 5 and I want 6th, 7th, 8th, 9th, 10th to have an increased speed to speed = 10 and etc. I already have some nested if statements,but I want to inscrease the speed infitely for example to have speed = 1000 for the 100th obstacle. Here's my code:

IEnumerator SpawnObstacles()
{
    int i = 0;
    do
    {
        var randomTime = Random.Range(minTime, maxTime);
        var randomObstacle = Random.Range(0, obstacles.Length);
        yield return new WaitForSeconds(randomTime);
        var obstacle = Instantiate(obstacles[randomObstacle], transform.position, Quaternion.identity);
        Destroy(obstacle, 10f);
        i++;
        if(i >= 10)
        {
            IncreaseSpeed();
            if(i >= 20)
            {
                IncreaseSpeed();
                if(i >= 30)
                {
                    IncreaseSpeed();
                }
            }
        }
    }
    while (true);
}
private void IncreaseSpeed()
{
        Obstacle obs = FindObjectOfType<Obstacle>();
        obs.speed += 5f;
}
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

The modulo operator % helps with things like this. It gives the remainder of the division operation, for example:

3%5 is 3
4%5 is 4
5%5 is 0
6%5 is 1
7%5 is 2
8%5 is 3
9%5 is 4
10%5 is 0

So if your x is starting at 0, and you want to increase speed every 5, you just look at whenever the modulo is 4..

x can go as high as you like (well, int.MaxValue) but the modulo will always be 0..4:

//do some stuff

//should we increase speed?
if(x % 5 == 4)
  IncreaseSpeed();

Handy tool for making circular arrays too.. someArray[i % someArray.Length] will never go out of bounds, because the result of a modulo is always between 0 and one less than the length

over 4 years ago · Santiago Trujillo Report

0

What you could do instead is either :

  • Keep track of how many obstacle instances you've spawned so far using a variable(maybe a static variable https://www.c-sharpcorner.com/UploadFile/1ce252/static-variables-and-static-methods-in-C-Sharp/ ) and make sure to simply increase the speed for every 5 instances using modulo %. You can take a look for more information here : https://blog.mattclemente.com/2019/07/12/modulus-operator-modulo-operation.html

A quick introduction to Modulos:

//It essentially shows the rest of a division
5 / 5 == 1 
//Since there's no rest there, 
5 % 5 == 0

//But in the case of where there is a rest
6 / 5 == 1 (Rest 1)
//Hence
6 % 5 == 1

//Other examples :
7 % 5 == 2
8 % 5 == 3
9 % 5 == 4
10 % 5 == 0 //!! The result is back to zero here as 10/5 == 2 with no Rest
  • Implement a recursive function that's in the Object that spawns in another object with the same attributes but pass on a parameter which indicates how many instances have been spawned so far and check if it's the 5th instance, if so add speed to it.
over 4 years ago · Santiago Trujillo Report

0

You have a Coroutine in an infinite loop anyway ... why not simply do

IEnumerator SpawnObstacles()
{
    do
    {
        for(var x = 0; x < 5; x++)
        {
            var randomTime = Random.Range(minTime, maxTime);
            var randomObstacle = Random.Range(0, obstacles.Length);
            yield return new WaitForSeconds(randomTime);
            var obstacle = Instantiate(obstacles[randomObstacle], transform.position, Quaternion.identity);
            Destroy(obstacle, 10f);
        }

        // These lines are automatically reached after every 5 iterations
        // no need for any módulo or other magic check
        Obstacle obs = FindObjectOfType<Obstacle>();
        obs.speed += 5f;
    }
    while (true);
}

Or since I'm not sure of this entire FindObjectWithTag thingy ... actually it seems to me it is the same object you Instantiate right? So why not simply

IEnumerator SpawnObstacles()
{
    var speed = 5;
    do
    {
        for(var x = 0; x < 5; x++)
        {
            var randomTime = Random.Range(minTime, maxTime);
            var randomObstacle = Random.Range(0, obstacles.Length);
            yield return new WaitForSeconds(randomTime);
            var obstacle = Instantiate(obstacles[randomObstacle], transform.position, Quaternion.identity);
            obstacle.GetComponent<Obstacle>().speed = speed;
            Destroy(obstacle, 10f);
        }

        // These lines are automatically reached after every 5 iterations
        // no need for any módulo or other magic check
        speed += 5f;
    }
    while (true);
}

Though to me it stays a bit unclear how exactly your speed shall grow. From your description it sounds like you want to add 5 after every group of iterations .. however this would mean you have speed = 100 (20*5) for the 100th object .. not speed = 1000 as you wrote

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!