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

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #14852
    Stephen Boling
    Participant

    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);
        }
    }
    #14881
    Terence
    Keymaster

    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.

    #14889
    Stephen Boling
    Participant

    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);
        }
    }
    #14892
    Terence
    Keymaster

    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 the target variable is null, then the statement becomes null.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 means currentChunk doesn’t exist). You missing the ! causes the function to run only when currentChunk is null, which causes a NullReferenceException in all of your if statements.

    #14894
    Stephen Boling
    Participant

    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?

    #14895
    Terence
    Keymaster

    You can move your Terrain Sorting Layer under your Default Layer. That way, all objects on the Default layer will appear above the Terrain layer.

    You should be able to find the setting to adjust the ordering of the layers under the Sorting Layer dropdown.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: