Wave Spawner Coding (Solved!!)

Updated 13 hours ago in Answers
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
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
9 on April 19, 2017


2 days ago

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

1 day ago

“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.

1 day ago

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

IEnumerator SpawnWave ()

Wave wave = waves[waveIndex];

EnemiesAlive = wave.count;

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



1 day ago

“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

15 hours ago

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.

14 hours ago

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?

14 hours ago

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

13 hours ago

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.

13 hours ago

No problem, happy to help!

Show more replies
  • Liked by