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

**URL:** <https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504>\
**Category:** 🎓 Courses & Articles\
**Created:** [March 4, 2016, 10:47am UTC](https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504 "2016-03-04T10:47:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gebirgsbaerbel](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/gebirgsbaerbel/32/87517_2.png) [@gebirgsbaerbel](https://forums.kodeco.com/u/gebirgsbaerbel)\
**Post date:** [March 4, 2016, 10:47am UTC](https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504/1 "2016-03-04T10:47:35Z")

</div>

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](https://www.raywenderlich.com/1623-how-to-create-a-tower-defense-game-in-unity-part-1)

---

<div class="post-metadata">

**Author:** ![kunhado](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kunhado/32/10015_2.png) [@kunhado](https://forums.kodeco.com/u/kunhado)\
**Post date:** [March 19, 2016, 5:21pm UTC](https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504/2 "2016-03-19T17:21:21Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![goldregin](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/goldregin/32/10127_2.png) [@goldregin](https://forums.kodeco.com/u/goldregin)\
**Post date:** [March 20, 2016, 6:54pm UTC](https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504/3 "2016-03-20T18:54:02Z")

</div>

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 😉  
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](http://scr.hu/6sn8/t4wrc)

---

<div class="post-metadata">

**Author:** ![gebirgsbaerbel](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/gebirgsbaerbel/32/87517_2.png) [@gebirgsbaerbel](https://forums.kodeco.com/u/gebirgsbaerbel)\
**Post date:** [March 21, 2016, 6:13pm UTC](https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504/4 "2016-03-21T18:13:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gebirgsbaerbel](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/gebirgsbaerbel/32/87517_2.png) [@gebirgsbaerbel](https://forums.kodeco.com/u/gebirgsbaerbel)\
**Post date:** [March 21, 2016, 6:19pm UTC](https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504/5 "2016-03-21T18:19:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![goldregin](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/goldregin/32/10127_2.png) [@goldregin](https://forums.kodeco.com/u/goldregin)\
**Post date:** [March 21, 2016, 10:03pm UTC](https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504/6 "2016-03-21T22:03:46Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![emossi](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/emossi/32/24793_2.png) [@emossi](https://forums.kodeco.com/u/emossi)\
**Post date:** [April 8, 2016, 4:33pm UTC](https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504/7 "2016-04-08T16:33:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![embel2](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/embel2/32/26391_2.png) [@embel2](https://forums.kodeco.com/u/embel2)\
**Post date:** [April 9, 2016, 12:20pm UTC](https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504/8 "2016-04-09T12:20:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gebirgsbaerbel](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/gebirgsbaerbel/32/87517_2.png) [@gebirgsbaerbel](https://forums.kodeco.com/u/gebirgsbaerbel)\
**Post date:** [April 10, 2016, 3:46pm UTC](https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504/9 "2016-04-10T15:46:22Z")

</div>

@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?

---

<div class="post-metadata">

**Author:** ![embel2](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/embel2/32/26391_2.png) [@embel2](https://forums.kodeco.com/u/embel2)\
**Post date:** [April 11, 2016, 4:01pm UTC](https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504/10 "2016-04-11T16:01:30Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![gebirgsbaerbel](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/gebirgsbaerbel/32/87517_2.png) [@gebirgsbaerbel](https://forums.kodeco.com/u/gebirgsbaerbel)\
**Post date:** [April 11, 2016, 6:13pm UTC](https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504/11 "2016-04-11T18:13:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![embel2](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/embel2/32/26391_2.png) [@embel2](https://forums.kodeco.com/u/embel2)\
**Post date:** [April 12, 2016, 3:19pm UTC](https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504/12 "2016-04-12T15:19:04Z")

</div>

@gebirgsbaerbel thank you very much,let me try this

---

<div class="post-metadata">

**Author:** ![kunhado](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kunhado/32/10015_2.png) [@kunhado](https://forums.kodeco.com/u/kunhado)\
**Post date:** [April 21, 2016, 2:00pm UTC](https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504/13 "2016-04-21T14:00:27Z")

</div>

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](http://www.souking.com.br/item/bio-defense)

---

<div class="post-metadata">

**Author:** ![gebirgsbaerbel](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/gebirgsbaerbel/32/87517_2.png) [@gebirgsbaerbel](https://forums.kodeco.com/u/gebirgsbaerbel)\
**Post date:** [April 24, 2016, 11:09am UTC](https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504/14 "2016-04-24T11:09:41Z")

</div>

Cool. I will try it 🙂

---

<div class="post-metadata">

**Author:** ![whit](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/whit/32/31982_2.png) [@whit](https://forums.kodeco.com/u/whit)\
**Post date:** [May 3, 2016, 6:57pm UTC](https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504/15 "2016-05-03T18:57:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gebirgsbaerbel](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/gebirgsbaerbel/32/87517_2.png) [@gebirgsbaerbel](https://forums.kodeco.com/u/gebirgsbaerbel)\
**Post date:** [May 4, 2016, 10:17pm UTC](https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504/16 "2016-05-04T22:17:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![whit](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/whit/32/31982_2.png) [@whit](https://forums.kodeco.com/u/whit)\
**Post date:** [May 5, 2016, 6:09pm UTC](https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504/17 "2016-05-05T18:09:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gebirgsbaerbel](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/gebirgsbaerbel/32/87517_2.png) [@gebirgsbaerbel](https://forums.kodeco.com/u/gebirgsbaerbel)\
**Post date:** [May 8, 2016, 10:48am UTC](https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504/18 "2016-05-08T10:48:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![banjochris](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@banjochris](https://forums.kodeco.com/u/banjochris)\
**Post date:** [May 8, 2016, 7:02pm UTC](https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504/19 "2016-05-08T19:02:11Z")

</div>

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!!!

---

<div class="post-metadata">

**Author:** ![eraeo](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/eraeo/32/32456_2.png) [@eraeo](https://forums.kodeco.com/u/eraeo)\
**Post date:** [May 12, 2016, 8:47pm UTC](https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504/20 "2016-05-12T20:47:11Z")

</div>

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.

[Next page](https://forums.kodeco.com/t/how-to-create-a-tower-defense-game-in-unity-part-1/6504.md?page=2)
