RTS Tutorial – Part XII Bugfix

Bug-fix

I just noticed a small bug while playing around – none of our units are able to move to an arbitrary point on the map any more. The problem lies in MouseClick() in Unit.cs. We added some code to handle clicking on an empty resource to allow the player to still move there. Unfortunately this is resulting in a NullReferenceException. The problem is actually that our Ground object has no parent, and so the call to hitObject.transform.parent.getComponent() fails. To fix this, we need to check whether the parent object exists.

if(hitObject.transform.parent) {
	Resource resource = hitObject.transform.parent.GetComponent< Resource >();
	if(resource && resource.isEmpty()) clickedOnEmptyResource = true;
}

Add in the if statement above and the problem is fixed.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s