Forum begins after the advertisement:


[part 2] Error Unity

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #13380

    Hello, I rewrote the code several times, redid it several times, even copied the code, but I can’t understand why this error appears when we bind to Up Down Left Right

    https://imgur.com/qv1O4Pa

    #13383
    void ChunkChecker()
     {
         if (!currentChunk)
         {
             return;
         }
    
         if (pm.moveDir.x > 0 && pm.moveDir.y == 0) //right
         {
             if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right").position, checkerRaduis, 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, checkerRaduis, 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, checkerRaduis, 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, checkerRaduis, 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, checkerRaduis, 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, checkerRaduis, 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, checkerRaduis, 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, checkerRaduis, terrainMask))
             {
                 noTerrainPosition = currentChunk.transform.Find("Left Down").position;
                 SpawnChunk();
             }
         }
     }
    #13385

    Is it possible that I didn’t distribute the area correctly?, but before making a change to the code, and when there were just cordianates, everything worked well, after I changed, here’s the error :(

    #13386
    Terence
    Keymaster

    It says that there is a NullReferenceException in line 67. I have to see line 67 of your code to pinpoint the issue to you specifically, but a NullReferenceException means that some object is not populated in your script.

    Take this line for e.g.:

    noTerrainPosition = currentChunk.transform.Find("Up").position;

    If currentChunk is null, it becomes null.transform.Find("Up") which will cause a NullReferenceException.

    #13388

    And I sent it to you, he swore at right down, but a little later I can send the full code because I’m not at home. And I’ll try what you wrote! But what’s strange is that even though I completely copied your code that you left under the lesson, it still caused this problem

    #13389

    I came home, I’ll send you a pack of screenshots, see what could be wrong, but it’s very strange that I’m the only one who encountered such an error :D

    View post on imgur.com

    #13394
    Terence
    Keymaster

    It seems like your “Right Up” GameObject cannot be found in line 76. So:

    currentChunk.transform.Find("Right Up").position

    becomes:

    null.position

    Check if your Right Up GameObject is spelt correctly, with the same case and everything.

    #13409
            else if (pm.moveDir.x > 0 && pm.moveDir.y > 0) //right up
            {
                if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right Up").position, checkerRaduis, terrainMask))
                {
                    noTerrainPosition = currentChunk.transform.Find("Right Up").position;
                    SpawnChunk();
                }
            }
    

    What could be wrong with this code? It’s just your written code, I took it ctrl + c ctrl +v, and I still don’t understand what it doesn’t like

    #13410

    The funny thing is, I removed the capital letter Right Up, but I still don’t want a square to appear on the left side, but the console doesn’t write anything, I tried to remove this capital letter from everyone, he started swearing at this line. In general, I don’t understand anything, but without that capital letter, it doesn’t write anything to the console, but it also doesn’t create endless locations for me, that is, I come to a gray surface

    https://imgur.com/a/PGl1Jid

    Could this be a problem because I’m using Unity 2022?

    #13411

    Could this be a problem because I’m using Unity 2022?

    #13413
    Terence
    Keymaster

    It’s very unlikely that this is a Unity 2022 issue. Can you show a screenshot of one of your Terrain Chunk prefabs?

    You can also try looking at this post and see if the tip here helps: https://blog.terresquall.com/community/topic/part-2-map-generation/#post-12273

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

Go to Login Page →


Advertisement below: