Forum begins after the advertisement:
[Part 2] NullReferenceException in MapController script
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 2] NullReferenceException in MapController script
- This topic has 5 replies, 2 voices, and was last updated 5 months, 4 weeks ago by Terence.
-
AuthorPosts
-
May 31, 2024 at 7:14 am #14852::
Good evening, I was told to post my Map Controller Script here
I have checked the code and am not seeing anything glaringly wrong, It also looks like I haven’t strayed from doing anything wrong in the editor
Additionally I have an issue where the TileMap is over the player even when I change the layers
Do you have any guidance?
public class MapController : MonoBehaviour { public List<GameObject> terrainChunks; public GameObject player; public float checkerRadius; Vector3 noTerrainposition; public LayerMask terrainmask; PlayerMovement pm; public GameObject currentChunk; // Start is called before the first frame update void Start() { pm = FindObjectOfType<PlayerMovement>(); } // Update is called once per frame void Update() { ChunkChecker(); } void ChunkChecker() { if (currentChunk) { return; } if (pm.moveDir.x > 0 && pm.moveDir.y == 0) //right { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right").position, checkerRadius, terrainmask)) { noTerrainposition = currentChunk.transform.Find("Right").position; SpawnChunk(); } } else if (pm.moveDir.x < 0 && pm.moveDir.y == 0) //left { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Left").position, checkerRadius, terrainmask)) { noTerrainposition = currentChunk.transform.Find("Left").position; SpawnChunk(); } } else if (pm.moveDir.x == 0 && pm.moveDir.y > 0) //up { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Up").position, checkerRadius, terrainmask)) { noTerrainposition = currentChunk.transform.Find("Up").position; SpawnChunk(); } } else if (pm.moveDir.x == 0 && pm.moveDir.y < 0) //down { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Down").position, checkerRadius, terrainmask)) { noTerrainposition = currentChunk.transform.Find("Down").position; SpawnChunk(); } } else if (pm.moveDir.x > 0 && pm.moveDir.y > 0) //right up { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right Up").position, checkerRadius, terrainmask)) { noTerrainposition = currentChunk.transform.Find("Right Up").position; SpawnChunk(); } } else if (pm.moveDir.x > 0 && pm.moveDir.y < 0) //right down { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right Down").position, checkerRadius, terrainmask)) { noTerrainposition = currentChunk.transform.Find("Right Down").position; SpawnChunk(); } } else if (pm.moveDir.x < 0 && pm.moveDir.y > 0) //left up { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Left Up").position, checkerRadius, terrainmask)) { noTerrainposition = currentChunk.transform.Find("Left Up").position; SpawnChunk(); } } else if (pm.moveDir.x < 0 && pm.moveDir.y < 0) //left down { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Left Down").position, checkerRadius, terrainmask)) { noTerrainposition = currentChunk.transform.Find("Left Down").position; SpawnChunk(); } } } void SpawnChunk() { int rand = Random.Range(0, terrainChunks.Count); Instantiate(terrainChunks[rand], noTerrainposition, Quaternion.identity); } }
May 31, 2024 at 7:01 pm #14881::Just to provide some context for anyone else reading this, Stephen has the following error on his Console:
NullReferenceException: Object reference not set to an instance of an object MapController.ChunkChecker () (at Assets/Scripts/MapController.cs:44) MapController.Update () (at Assets/Scripts/MapController.cs:24)
Stephen, I need a little more information. Can you highlight lines 44 and 24 on your script for me? You can bold both lines.
June 1, 2024 at 7:30 am #14889::Good evening Terrence, So I’m not getting just that one error for those lines, its for a bunch of the other lines as well, but pretty much its the if statements within the Chunk Checker
public class MapController : MonoBehaviour { public List<GameObject> terrainChunks; public GameObject player; public float checkerRadius; Vector3 noTerrainposition; public LayerMask terrainmask; PlayerMovement pm; public GameObject currentChunk; // Start is called before the first frame update void Start() { pm = FindObjectOfType<PlayerMovement>(); } // Update is called once per frame void Update() { ChunkChecker(); } void ChunkChecker() { if (currentChunk) { return; } if (pm.moveDir.x > 0 && pm.moveDir.y == 0) //right { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right").position, checkerRadius, terrainmask)) { noTerrainposition = currentChunk.transform.Find("Right").position; SpawnChunk(); } } else if (pm.moveDir.x < 0 && pm.moveDir.y == 0) //left { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Left").position, checkerRadius, terrainmask)) { noTerrainposition = currentChunk.transform.Find("Left").position; SpawnChunk(); } } else if (pm.moveDir.x == 0 && pm.moveDir.y > 0) //up { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Up").position, checkerRadius, terrainmask)) { noTerrainposition = currentChunk.transform.Find("Up").position; SpawnChunk(); } } else if (pm.moveDir.x == 0 && pm.moveDir.y < 0) //down { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Down").position, checkerRadius, terrainmask)) { noTerrainposition = currentChunk.transform.Find("Down").position; SpawnChunk(); } } else if (pm.moveDir.x > 0 && pm.moveDir.y > 0) //right up { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right Up").position, checkerRadius, terrainmask)) { noTerrainposition = currentChunk.transform.Find("Right Up").position; SpawnChunk(); } } else if (pm.moveDir.x > 0 && pm.moveDir.y < 0) //right down { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right Down").position, checkerRadius, terrainmask)) { noTerrainposition = currentChunk.transform.Find("Right Down").position; SpawnChunk(); } } else if (pm.moveDir.x < 0 && pm.moveDir.y > 0) //left up { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Left Up").position, checkerRadius, terrainmask)) { noTerrainposition = currentChunk.transform.Find("Left Up").position; SpawnChunk(); } } else if (pm.moveDir.x < 0 && pm.moveDir.y < 0) //left down { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Left Down").position, checkerRadius, terrainmask)) { noTerrainposition = currentChunk.transform.Find("Left Down").position; SpawnChunk(); } } } void SpawnChunk() { int rand = Random.Range(0, terrainChunks.Count); Instantiate(terrainChunks[rand], noTerrainposition, Quaternion.identity); } }
June 1, 2024 at 2:56 pm #14892::Stephen, whenever you have a NullReferenceException, it means that one of the objects in the line you are trying to access the property of is null. For example, if you do
target.position
and thetarget
variable is null, then the statement becomesnull.position
, which is what causes the error.In your case here, at the start of the
ChunkChecker()
, you are missing a!
character:void ChunkChecker() { if (!currentChunk) { return; } ...
The statement is there to terminate the function early if
currentChunk
is null (i.e.!currentChunk
meanscurrentChunk
doesn’t exist). You missing the!
causes the function to run only whencurrentChunk
is null, which causes a NullReferenceException in all of your if statements.June 2, 2024 at 5:02 am #14894::Oh Dang ok that worked perfectly
So my next question while I’m here is that my player character is stuck underneath the terrain.
I currently have the terrain in the terrain layer, while I have my player in the default layer, do you have any idea what might be causing that?
June 2, 2024 at 11:45 am #14895 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: