I’ve been following the course so far up to the implementation of the Level Grid and I’ve hit my first snag. I’ve rewatched the video a few times but haven’t been able to pinpoint why exactly the Unit returns null when it’s called in Grid Object’s ToString function. To demonstrate what is happening:
Now, I’ve tried to isolate that it wasn’t something completely silly so I tested that it wasn’t a case of the text on the new line getting cut off. Logging the unit also confirmed that it’s returning null. By using _unit.name, I was also able to get a breadcrumb trail of where the problem may be coming from… but this is where I’m lost.
It doesn’t help that I’ve not 100% understood the implementation in this video. In past videos, I’ve been able to take on the challenges and figure them out on my own, but the challenge in this video is where I started to draw a blank. So it’s completely possible that I’m overlooking something major because of that. I figured a fresh pair of eyes would help me see what I’m missing.
As for what I’ve tried so far:
- Since I got the error that the reference was not set to an instance of an object error, I’ve doublechecked all of my game objects and confirmed that nothing is missing its respective reference/prefab.
- Prior to this video, all unit/grid/mouse functionality was working as intended so my best guess is that the problem is isolated to one of the scripts covered in this video. But after several passes through the content, I’ve not been able to spot where my scripts and the video’s scripts differ.
- In my scripts, I did change some naming conventions. Mostly, it was stuff like using underscores for private variables because I was starting to lose track of what was an argument being passed in and what was a private class variable. I’ve used both Visual Studio’s refactor tool and reference search tool to try to see if it was a simple typo issue. But, as far as I’ve spotted, everything should be about the same.
Still, here is a screenshot of my scripts just in case it really is just a typo issue or if I mistakenly left out a line of code:
This is what I understand is happening, and is maybe what’s going wrong:
- In Level Grid’s Awake function, a new Grid System is created (a 10 by 10 group of cells that are 2f in size).
- A new Grid System is created — instantiating a new Grid Object array, creating a new Grid Position for each cell in the (in this case) 10 by 10 group of cells, and creating a Grid Object at each position.
- Level Grid also calls Level System’s CreateDebugObjects function which also creates a new Grid Position for each cell in the specified number of cells and instantiates a debugPrefab object (which has a TextMeshPro component). It then sets a private variable to reference the GridObjectDebugger script on each object and sets the Grid Object for each one.
- In the GridObjectDebugger script itself, the respective Grid Object is set by the called SetGridObject function along with the TextMeshPro’s text field (to whatever Grid Object’s ToString field is).
- Grid Object’s ToString string is set to both whatever that Grid Object’s Grid Position ToString field is set to plus the name of the unit on a new line.
- Each Grid Position has already been created and the ToString string has been set to whatever x and z value it was instantiated with. And that is confirmed to show correctly on the debugPrefab text.
I’m getting lost in the sauce, but bear with me. My guess is that in this wild turn of events, each Grid Object is not receiving a reference to a unit before the debugPrefab’s text is set by checks notes Level Grid (?) when it creates each debug object. But:
- In the Unit script, a private Grid Position reference is created using the Level Grid singleton’s GetGridPosition function which should pass the unit’s current position into Level Grid’s private Grid System and return a new Grid Position based on the x and z values it was given. This new Grid Position is saved as a private variable by the Unit script.
- The Unit script then moseys up to the Level Grid singleton again to use the SetUnitAtGridPosition function passing in the private Grid Position just created and itself. Said function creates a private Grid Object reference that uses Level Grid’s private Grid System reference to pass the Unit’s private Grid Position reference into the GetGridObject function.
- The aforementioned function returns a reference to Grid System’s private Grid Object Array (at the coordinates passed in as arguments) … uh. Hold on. Let me see where I’m at. Actually, this is where I’m lost.
Now, again, it’s possible that this error is just a typo or something stupid. But, since I don’t wholly understand what’s going on in the code, I’d really appreciate if anyone could explain the steps I’m misunderstanding.
EDIT: I searched the references to the SetUnit function in the Grid Object script. _unit should be set to an instance of a unit by the SetUnitAtGridPosition function or set to Null by ClearUnitAtGridPosition. So… I’m maybe back to the drawing boards entirely. Technically, most cells should return null so that’s not really an error. But there are at least two units in my scene right now so 2 out of 100 cells should return a unit rather than null… but they aren’t. And I still don’t know why.