Tower Instantiation Broke

When I tested following the script updates to the Tile, PathFinder and GridManager scripts my towers were no longer instantiatiating with the OnMouseDown method. In addition to the expected error of the Enemy not being able to find its path, I also get a NullReferenceException pointing to the Tile script:

Screenshot 2021-08-17 185609

And here’s the code block at line 37:

Thinking that I had somehow unknowingly messed up the Tile prefab, I double checked that the Tower prefab was still in the serialized field in the Inspector (it is):

Then to be sure there wasn’t anything wrong with the tower prefab itself, I checked that. It appears to be fine as well:

Screenshot 2021-08-17 184828

I have to admit, because I’m not really understanding what’s happening in this section, I have no idea where else to look to resolve the error. Any guidance would be appreciated.

Hi Charly,

At the end of the video (“Valid Path”), there is a NullReferenceException error message in Gary’s console. He fixes that problem in the next video. If you did everything like he and compared your code to his, it might be that the next video will help you fix this.

Please let me know if Gary’s solution in the next video solved the problem for you. :slight_smile:

It did resolve the error, but I have no idea why it worked that way. The tower script wasn’t touched at all, nor does it have a Awake, Enable, or Start method that would be impacted by the Script Execution Order?

The problem is/was the following: Unity calls methods in an arbitrary order. We cannot know in which order the Awake methods are getting called during runtime. Since our logic (pathfinding, GridManager, etc.), depend on one another, we have to ensure that Unity executed our methods in the correct order, so the GridManager generates a grid before the Pathfinder calculates a path. Otherwise, the Pathfinder works with the wrong data. And in that case, it might be that you are not able to place towers anymore.

Why the NullReferenceException? In line 37, you have: gridManager.GetNode(coordinates). With the wrong grid, the method might return null because no such key was found in the grid dictionary. And if you try to access isWalkable on a non-existing object, a NullReferenceException error will get thrown.

As aforementioned, the execution order is arbitrary. That’s why it might be that everything was working, then you did something unrelated and suddenly it stopped working.

Hopefully, this made sense. :slight_smile:

I think I’m following what you’re saying here, but to be sure, I interpret your statement to mean that the error is tossing me out by the line in the red rectangle, not the line in the white rectangle.

However, if the ‘coordinates’ object was assigned from an Update method (not advisable I know, but if one did it in theory) then the error wouldn’t occur because many frames would’ve passed before the player tried to place a tower, and the coordinates would have been passed in several times over regardless of how the Script Execution Order was set up. Am I on the right track?

Yes, you might be on the right track. I’m just not sure if I understood your reasoning correctly. Given gridManager is not null, the question is: Is GetCoordinatesFromPosition able to return null? If it’s not, the next question is: Is GetNode able to return null? And then: If you call GetCoordinatesFromPosition and assign the value to coordinates, will GetNode always return a Node object? (By “always”, I mean “always as long as you don’t delete the item from the dictionary”.)

Depending on the answers (yes, yey, no), there is a potential problem. Or there isn’t.

To figure out if your solution does not return the expected result by chance but actually solves the problem, you need to make sure that all “chain links” in your logic chain do exactly what you expect them to do.

Add Debug.Logs to your code to figure out more. And test your code. If it works, it works. :wink:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms