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/269-how-to-create-a-tower-defense-game-in-unity-part-1

Thank you for the great tutorial. But I have a problem. After steps “Test Upgrade Capability” and “Enable Upgrading With Gold” when I try to upgrade monster game crashes at line:

MonsterLevel nextLevel = monsterData.GetNextLevel();

with error:

NullReferenceException: Object reference not set to an instance of an object
PlaceMonster.CanUpgradeMonster () (at Assets/PlaceMonster.cs:54)
PlaceMonster.OnMouseUp () (at Assets/PlaceMonster.cs:40)
UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)

As I understand monsterData is null and this method GetComponent doesn’t work properly in my case:

MonsterData monsterData = monster.GetComponent();

Spend several hours trying fix it by myself but I couldn’t find a solution. I will be grateful for the advice in solving this problem.

P.S. I’m using Unity 2017.2.0 p4

@jayfeesh Can you please help with this when you get a chance? Thank you - much appreciated! :]

Glad you are enjoying the tutorial!

Be sure to include the null check:
if (monster != null) { // This null check should prevent the exception
MonsterData monsterData = monster.GetComponent();
MonsterLevel nextLevel = monsterData.GetNextLevel();
if (nextLevel != null)
{
return true;
}
}

@jayfeesh

NullReferenceException: Object reference not set to an instance of an object
SpawnEnemy.Start () (at Assets/Scripts/SpawnEnemy.cs:13)

I keep getting this error, when trying to make the enemy move to the waypoints

Would love help

@jayfeesh Can you please help with this when you get a chance? Thank you - much appreciated! :]

@dobbie1324 Be sure your Waypoints are set in the Editor otherwise you will get a NullReferenceException!

https://koenig-media.raywenderlich.com/uploads/2015/07/waypoints2.gif

Please include code snippets if this does not fix your issue. Thanks!

@jayfeesh
using UnityEngine;
using System.Collections;

public class SpawnEnemy : MonoBehaviour
{

public GameObject[] waypoints;
public GameObject testEnemyPrefab;

// Use this for initialization
void Start()
{
	Instantiate(testEnemyPrefab).GetComponent<MoveEnemy>().waypoints = waypoints;
}

// Update is called once per frame
void Update()
{

}

}

The waypoints are set, this is the code

Thanks for the help :slight_smile:

@dobbie1324 If your waypoints are set, also ensure testEnemyPrefab is set. Then make sure testEnemyPrefab has the ‘MoveEnemy’ script attached.

Hopefully that fixes it!

@jayfeesh
How do i know if the script is attached

When you click on the GameObject, and look in the Inspector window. You should see the script MoveEnemy in the inspector along with the GameObjects other components like its Transform.

I’m working on my own little tower defense game for my assignment in my course and I’ve been following this tutorial to create the script for me to use to code my game. I’ve been having issues though. After I implemented the waypoints, when I tested out to see if they work, my enemy always gets stuck at the first waypoint that it reaches. This has troubled me so much and I can’t figure out what went wrong. Not even my professor couldn’t figure out what happened so he suggested for me to contact you for help. Here is the sharable link to my game. Do you think you can help me figure out what went wrong and how to fix it?

Thank you for your time and I’ll be looking forward to hearing back from you.

Never mind. We figured it out.

The wave maker seems buggy. Second wave doesn’t start for a good 30 seconds after the “Next Wave” animation, and when I traced the output, I noticed the interval count reseting before it hit the 2.5 second mark over and over, until some indeterminate point when it passed it and kept going. My time between waves is only 5 seconds.

This brings me to a broader question: why is there so much code in the Update() function for a lot of these things? Isn’t it considered better to use Coroutines these days?

I did find the issue – it seems like there was a strong dependency on the individal enemies’ “lastWaypointSwitchTime” variable, which I was no longer keeping track of because I changed how the movement code worked (I now use Time.deltaTime instead of Time.time, since I implemented an enemy blocking feature where enemies have to fight their way through towers/monsters before moving on). Still makes me want to rewire the logic to use Coroutines instead of that very long if statement in Update().

This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!