- Home /
Problem is not reproducible or outdated. I ended up finding another approach that was completely different from original problem
node null reference
So like most on here I am very new to coding and am still wrapping my head around some of the ins and outs of c#. I am attempting my first crack at my own game. I am using some code from a tutorial to make the grid and path-finding and node system, with my own tweaks added in for my purpose. Everything works up until now. I have a working grid that my player can travel on. I have made each node a large 10x10 area space and am using each node as a static room. I am trying to write a script to spawn a room in the next node as the player gets to a collider i set at edge of the node. I have a door that opens from the same collider so I know its working.
The call to the path finder code works cause its getting called to make my player move from node to node. I keep getting a null reference error when I try to use the node of object. The debugging is me making sure that the curRoomNode has a value right before I call the GetNeighbours() method.
I am using this code so far...
void SpawnRandomRoom()
{
//spawns item in array position between 0 and number of rooms in list
int whichRoom = Random.Range(0, listOfRooms.Count);
GameObject myObj = Instantiate(listOfRooms[whichRoom]) as GameObject;
numRoomsSpawned++;
// Find current node that room is on and get neighbours
Vector3 curRoomWorldPos = transform.position;
Debug.Log("world pos " + curRoomWorldPos.x+", "+ curRoomWorldPos.y+", "+curRoomWorldPos.z);
Node curRoomNode = GridBase.singleton.GetNode((int)curRoomWorldPos.x,(int)curRoomWorldPos.y,(int)curRoomWorldPos.z);
Debug.Log("Current node is:" + curRoomNode.x + "," + curRoomNode.y + "," + curRoomNode.z);
List<Node> curNeighbours = new List<Node>();
foreach (Node neighbour in pathfinder.GetNeighbours(curRoomNode))
{
curNeighbours.Add(neighbour);
}
Debug.Log("The neighbours are: " + curNeighbours);
if(gameObject.name == "Door West")
{
//myObj.transform.position =
}
//myObj.transform.position = transform.position;
}
In the froeach part It calls to get neighbour nodes. but it keeps giving me the nullreferenceexception error. I have tried a few other ways to call the node to get the neoighbours and all keep giving me the same error. Am I using the curRoomNode variable wrong?
Wild guess but I suppose
Node curRoomNode = GridBase.singleton.GetNode((int)curRoomWorldPos.x,(int)curRoomWorldPos.y,(int)curRoomWorldPos.z);
isn't returning anything on that x,y,z position. So curRoomNode is null.
When I play in unity in the console the debug log right after that shows me the proper node coordinates. So it is populated
is pathfinder a variable or a static class? $$anonymous$$aybe its not initialized if its used as a variable.
if the pathfinding searches out past the size of your nodes/rooms. it would cause this error. hopefully your pathfinding code has a line to be sure it is not cheking indexes higher than the size of your list.
Answer by madks13 · Jul 27, 2018 at 03:23 PM
I'd say your pathfinder can't find any neighbours. Can you post the GetNeighbours method? OR at least check what values your node properties have in the debugger?
I'd assume GetNeighbours returns an IEnumerable, which would be empty if nothing is found. The foreach wouldn't execute in that case. Unless he returns null specifically.
So this morning I ended up going with a clumsier way about it. Which also tells me that the curRoomNode was populated...
Node curRoomNode = GridBase.singleton.GetNodeFromWorldPosition(curRoomWorldPos);
Debug.Log("Current node is:" + curRoomNode.x + "," + curRoomNode.y + "," + curRoomNode.z);
// Depending on which door Collider the player touches spawn room in the direction.
if (gameObject.name == "Door West")
{
Vector3 nodeWorldCoordinates = GridBase.singleton.GetWorldCoordinatesFromNode(curRoomNode.x - 1, curRoomNode.y, curRoomNode.z);
Debug.Log("The World Coordinates from node position is: " + nodeWorldCoordinates);
GameObject myObj = Instantiate(listOfAllRooms[whichRoom], new Vector3 (nodeWorldCoordinates.x - 1, nodeWorldCoordinates.y, nodeWorldCoordinates.z), Quaternion.identity) as GameObject;
myObj.name = listOfAllRooms[whichRoom].name;
}
its not so elegant but its working Wow this code formatting is hard to get right on this forum eh
The path finder has to be working cause I can move around the grid, which is why i was trying to use the get neighbors method for this spawning purpose. Heres the get neighbors snippet...
public List<Node> GetNeighbours(Node node, bool getVerticalneighbours = false)
{
//This is were we start taking our neighbours
List<Node> retList = new List<Node>();
for (int x = -1; x <= 1; x++)
{
for (int yIndex = -1; yIndex <= 1; yIndex++)
{
for (int z = -1; z <= 1; z++)
{
int y = yIndex;
//If we don't want a 3d A*, then we don't search the y
if (!getVerticalneighbours)
{
y = 0;
}
if (x == 0 && y == 0 && z == 0)
{
//000 is the current node
}
else
{
Node searchPos = new Node();
//the nodes we want are what's forward/backwars,left/righ,up/down from us
searchPos.x = node.x + x;
searchPos.y = node.y + y;
searchPos.z = node.z + z;
Node newNode = GetNeighbourNode(searchPos, true, node);
if (newNode != null)
{
retList.Add(newNode);
}
}
}
}
}
return retList;
}
A few things i've been wondering : 1. Are all the rooms in rectangular shape? 2. Can the player exit any room from any side or are there some restrictions? 3. Are all rooms of same size?
So what I've done is take a a regular node and grid creation and make each node a 10x10 square. I am using each node as a room space. I have 1 room spawn when grid gets created in the center and then when the player goes to next grid I'm trying to make a new random room from a list spawn. All rooms are square 10x10 and will have different number of doors depending on room that gets spawned. All doors will be at center of edges to join to next room. I've been stumped on the random ro generation till a few days ago. Now I'm trying to figure out how to get the door system coded so that the room will rotate to match up doors to doors. It's a work in progress but basically the players create a new map everytime they play through the game. It's a board game style game so the the movement is very linear from room to room and not moving around in each room.
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How do I nullreference this correctly? 1 Answer
How to Check if a component Exists on a Gameobject. 3 Answers
NullReferenceException error in an array of objects 0 Answers
Multiple Cars not working 1 Answer