How to Create a Tower Defense Game in Unity – Part 2

hi thanks for your example . my problem is : time between waves not work in spawn enemy

I have a similar question as @minnejo but instead of switching between an array of enemies and spawning by switching; I want to create a second instance of the SpawnEnemy.cs that has a separate set of monsters and spawn rates
-also that spawn in the same instance at the same time?

Or easier, can I just do something like this inside SpawnEnemy?
"…
Public class wave1 { code }
Public class wave 2 {same code}

public class SpawnEnemy: Monobehaviour {
public Wave[] waves1;
public Wave[] waves2;
…}"

Thanks! I’m just trying to expand off of the tutorial a bit.

can i use the resources here for my game? tia

How would you randomize the different kinds of enemies? Also how would you make different types of towers?
Thanks! I am doing a tower defense game for my Senior Project to graduate, so a quick reply would be appreciated. I am using this as a base model for my personal game.

First of all, as the balance of tower defense games is quite tricky, random enemies are usually not a good idea. If you really want random enemies however, you could just create a list with all prefabs of enemies that could be in that wave, instead of just one prefab. Before you create a new enemy you then every time randomly pick one of the prefabs to create it.

For different types of towers, you do the same, as we did for our one tower. Now you need some menu, where the user can select, which tower they want, or again, you just build one randomly :wink:

but how would I set it to do random enemies do i set each enemy a number or just the name?
thanks again

I am also having an issue with the health indicator… the little green guys will disappear at the end but not once a bug gets to the cookie what am I doing wrong? I am following the tutorial completly the way it says

Now I feel like an idiot i missed one ending bracket and thats what my issue was

Do not feel stupid, forgeting brackets has happened to even the best of us.
About picking a random enemy: Currently each wave has only one variable to store one enemy prefab. Make this into an array. Put the different types of enemy prefabs in there, that should be in the wave. Then randomly create a number between 0 and the size of this array. Create an enemy from the prefab at this index.
Have fun with your project :slight_smile:

HI Barbara, I just finished the game based on your tutorial ( It is my uni project actually), I added more levels and some other stuff to make it more interesting. I really want to thanks you for providing this tutorial. You can search “bugs Invaders” in google play.Thank you once again.

Hi @nadz I just see this now. I am incredibly happy my tutorial could help you with your uni project. Keep up learning, there is tons of fun things you can do with unity :slight_smile:

plss help me I want to change the enemies but I always Have an error
NullReferenceException: Object reference not set to an instance of an object
MoveEnemy.RotateIntoMoveDirection () (at Assets/MoveEnemy.cs:59)
MoveEnemy.Update () (at Assets/MoveEnemy.cs:33)

and this is the code that get an error:
gameObject.transform.FindChild(“Enemies”).gameObject;
what should I do? thanks

@jpainza From what I can tell, it seems that you did not attach a child named “Enemies” to your new enemy. Once you do that, Unity should be able to find the this gameObject and not crash. At the moment it crashes, because it did not find a child named “Enemies”.

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]