# Wave Spawner Coding (Solved!!)

Updated on April 30, 2017 in
10 on April 19, 2017

I need help with coding wave spawner in tower defense game.
I’m trying to make infinite waves and increasing difficulty by increasing number of enemies when all different types of waves are complete.
How do I increase the number ‘count’ in wave script automatically?

• Liked by
Reply
on April 19, 2017

i++

on April 28, 2017

what do you mean by i++?
Can you explain in more detail?

on April 29, 2017

“i”  is the standard variable name used for incrementing a number (counting). In your case, you seem to have a variable called ‘count’, so count++; would accomplish ‘increasing the number in the script’. Wherever you have a wave ending, increment your counter. An integer variable followed by ++ increases it’s value by 1 (after the variable is used. If you were to have “++count” it would increment before use).

```int i = 0;
Debug.Log(i++); //should print 0
i = 0;
Debug.Log(++i); //should print 1
```

To increment a value is essentially the same as setting the value to itself plus 1.

on April 29, 2017

but it says: “The name ‘count’ does not exist in the context”.

IEnumerator SpawnWave ()
{
PlayerStats.Rounds++;

Wave wave = waves[waveIndex];

EnemiesAlive = wave.count;

for (int i = 0; i < wave.count; i++)
{
SpawnEnemy(wave.enemy);
yield return new WaitForSeconds(1f / wave.rate);

}

count++;
waveIndex++;
}

on April 29, 2017

“What do you mean by i++”
-(has a for loop with i++)

‘count’ does not exist in the context because you don’t have a variable named ‘count’. I thought you did when you originally stated “How do I increase the number ‘count’ in wave script automatically?”. I really am not trying to sound like an ass, but do yourself a favor and learn what the code means, don’t just type along with tutorials. Read the code, see what it does piece by piece.

If you want the waves to be infinite (the same 20(?) waves) just set the waveIndex to 0 every time it reaches the 20th(if that’s the last) wave. I haven’t watched this series, so I don’t know how the enemies are implemented. I can’t tell you exactly how to make them harder. If ‘wave’s hold a number of enemies value, you can have something similar to this

Depending on how the Wave class is laid out

```int difficulty = 0; //to hold how many times you've reset the wave index
wave.count += difficulty * 5; //this will add 5(per difficulty) more enemies to the wave
```

on April 29, 2017

the variable named ‘count’ is assigned in another script. But I thought about changing the value of the variable in wave index called ‘count’. In another script, it contains int variable called ‘count’ and I assign a value at the inspector. What I wanted to do is; increasing the value of ‘count’ each time a player finishes all the waves. I already assigned the code to set waveIndex = 0; each time when the player finishes all the waves. In the index; it contains elements and within the element, contains the variable called ‘count’. Now, I can modify the value of ‘count’ via inspector but I want to know how to modify value ‘count’ in the script right where the script checks if the player finished the waves. I thought you have checked the script of the tutorial which I included before you answer my question.

on April 29, 2017

The count which you are talking about it an attribute of the wave object you created. You are already accessing it using wave.count. Now how do you assign a new value to it? You should be able to do this because you are already doing variable assignment in the code you provided. Read and understand the code you are copying from the tutorial.
Think about it like this. If wave.count is the variable you want to change, how could you add 1 to it?

on April 29, 2017

I’m thinking same as adding waveIndex value by doing waveIndex++;
So I will put it as wave.count++;

on April 29, 2017

wow, for now, I added wave.count++; after spawning all enemies in a wave which only affects specific wave. In the future, probably next week, I will add difficulty variable so I can add more difficulty so each time adds more than one enemy and possibly increasing health and speed. There is just so much I can do.
Now, I can move on to adding high score system. Thank you, Billy Bowden for taking your time to explain this to me.

on April 30, 2017

No problem, happy to help!

Show more replies
• Liked by
Reply
YOUR PREVIEW