Kodeco Forums

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

In this tutorial, you'll build a 2D tower defense game using the latest Unity engine.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1623-how-to-create-a-tower-defense-game-in-unity-part-1

Congratulations on the tutorial , I found incredible and extremely didactic their work !!! I am Brazilian , and this tutorial is helping me a lot , so my game is ready, post the results to you .
Thank you very much! See you…

Hello!
I really enjoy your tutorial buy I have error that I can’t fix myself. I did the part with SpawnEnemy script but I doesn’t work. The Enemy doesn’t appear at all. If you could help me it would mean a world to me :wink:
I get this message in console everytime I run the game.
NullReferenceException: Object reference not set to an instance of an object
SpawnEnemy.Start () (at Assets/SpawnEnemy.cs:12)
here is a screenshot of my work - http://scr.hu/6sn8/t4wrc

Looking forward to your finished game. I will definitely play it when you post it :slightly_smiling:

1 Like

Hi goldregin,
unfortunately the screenshot does not seem to work properly. I only get a 404.
However I have a good guess as to why it might not be working.
The error probably occurs in the following line:

gameManager = GameObject.Find("GameManager").GetComponent();

The problem would occur, because you do not have a game object named “GameManager” or you misspelled “GameManager” in Find().
Let me know, if this helped.

My brother helped me fix this issue. The problem was in Enemy prefab. I didn’t add the MoveEnemy script as a component to Enemy (in prefab folder).
Btw Thanks for answering to my comment :slightly_smiling:

Thanks for showing a good Tower Defense Tutorial.
I’m Korean and have a poor English. However, your explain is awesome and I can understand and read there.
Hope showing more tutorial with Unity.
Thank you.

thanks to the tutorial
but I have a problem
Can you give a tutorial for select monster first before built that
thanks
sorry for my poor english

@embel2 I am not certain, what you mean by “selecting monster before built that”.
What is it I should have built. Can you maybe tell me which part of the tutorial you need help with?

@gebirgsbaerbel i’ve completed the tutorial
i just want add some features like when you click on Openspot,it’ll appear some option for select deferent tower

@embel2 You could create a selection like this:

  • Create a menu using one sprite per tower.
  • Add a collider onto each of those sprites. You can use these sprites as buttons, just as you did with OpenSpot.
  • Create a script for the button that takes a prefab for a tower and a reference to the OpenSpot that was clicked. When the button is clicked set the OpenSpot and instantiate a new tower prefab, which you then assign to OpenSpot.
  • Create a script that opens the menu by setting the buttons active and inactive when the player clicks on OpenSpot. Also move the menu to appear at the position of the OpenSpot.

This is a rough outline of what you need to do, hope it helps.

@gebirgsbaerbel thank you very much,let me try this

Hi @gebirgsbaerbel, I finished the game based on your tutorial, and even mentioned in the credits you hehe.
So I would like to show you the game , it is available here http://www.souking.com.br/item/bio-defense

Cool. I will try it :slight_smile:

Hi @gebirgsbaerbel I am using your tutorial as a bases for my high school game design final project but am having an error that I haven’t been able to fix. In Unity the error log reads,

NullReferenceException: Object reference not set to an instance of an object
PlaceMonster.canPlaceMonster () (at Assets/Scripts/PlaceMonster.cs:17)
PlaceMonster.OnMouseUp () (at Assets/Scripts/PlaceMonster.cs:25)
UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32).

the actual line of code it is talking about is

return monster == null && gameManager.Gold >= cost;

Everything else in the tutorial game is finished and works fine ,great game btw, I just cant get this gold deduction to work.

Hope this isn’t too much of a hassle and thanks in advance for any help you can give.

Hi @whit,
most likely the problem is, that you forgot to assign a value to gameManager. Because gameManager is null, you then get an error. Check, whether you assign a value and how you do it.
To find out, whether gameManager is indees null, you can use breakpoints, this can help greatly.

The problem was that I accidentally didn’t capitalize start on Void Start(), ops always ends up being something simple like that with me. But you pointed me towards the right set of code to look at so thanks for the help.

I am happy, you could resolve the issue. Those little mistakes are made by nearly every programmer occasionnally, so the important thing is that you can find and repair them.

Great Tutorial So Far!

Looking forward to completing Part II. Had a few issues along the way, but mostly fat fingers and reading too fast.

Thanks!!!

I really want to do this tutorial, but when I believe I’ve completed the first bit of coding in MonoDevelop, clicking on the x’s does nothing! I haven’t the slightest idea where I’ve gone wrong.