Forum begins after the advertisement:


[Part 2] Problem with spawning chunks diagonally

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 2] Problem with spawning chunks diagonally

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #15748
    Max Casas
    Level 4
    Participant
    Helpful?
    Up
    1
    ::

    Hello everyone,

    I’m following the tutorial for a school project, and I’ve encountered some issues related to chunk generation in my game. Everything is almost working correctly, but I’m facing a few problems:

    When I start the game and move in the directions up, down, right, left, top-left, and bottom-right, the chunks generate successfully. However, I’m having trouble with the bottom-left and top-right directions, where the chunks don’t generate as they should.

    Another issue is that only one chunk is generated when moving. Once I move out of that new chunk, no more chunks are generated, even though the optimization seems to be correct.

    I’ve searched through other discussions and tried various solutions, but nothing has worked (or I’ve just gotten tired of searching). I double-checked the spelling in the code, and everything seems to be correct, but I keep getting an error that points me to a specific line of code, and I can’t figure out what’s wrong.

    The error is this:

    NullReferenceException: Object reference not set to an instance of an object
    MapController.ChunkChecker () (at Assets/Scrips/MapController.cs:85)
    MapController.Update () (at Assets/Scrips/MapController.cs:32)

    This line is the 85: noTerrainPosition=currentChunk.transform.Find(“Left down”).position;
    And this is the 32: ChunkChecker();

    I would really appreciate any help, as this project is very important to me. (I’m also a bit frustrated because my teacher asked us to make a game even though she never taught us anything about Unity, and my grade depends on this tutorial).

    Thank you very much in advance!

    I will post a video of the issues that im having in a moment
    And i’m sorry about this, but I don’t know nothing about Unity, that’s why I’m having these problems:(

    has upvoted this post.
    #15749
    Max Casas
    Level 4
    Participant
    Helpful?
    Up
    1
    ::

    View post on imgur.com

    There is the link
    I already saw the video of the part 11, but I think that is not the same problem that I Have
    Thank you!

    has upvoted this post.
    #15752
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Max, let’s try and fix the NullReferenceException first. These errors sometimes block the rest of your code from operating normally. In your case, you can try adding the following lines of code:

    print(currentChunk);
    print(currentChunk.transform.Find("Left down"));
    noTerrainPosition=currentChunk.transform.Find("Left down").position;

    Then, check your console and if you get any output above your NullReferenceExceptions. The exceptions happen because either currentChunk isn’t assigned, or the Left down GameObject isn’t there in some of your chunks. We need to find out which one is it.

    Can you also post the entire script for MapController here?

    #15755
    Max Casas
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    Hi! Thank you so much for helping me
    Here’s the deal:
    When I press DOWN LEFT, gives me this error:

    NullReferenceException: Object reference not set to an instance of an object
    MapController.ChunkChecker () (at Assets/Scrips/Map/MapController.cs:87)
    MapController.Update () (at Assets/Scrips/Map/MapController.cs:32)

    The code segment is this one:

       noTerrainPosition=currentChunk.transform.Find("Left down").position;

    When I press UP RIGHT, gives me this:

    NullReferenceException: Object reference not set to an instance of an object
    MapController.ChunkChecker () (at Assets/Scrips/Map/MapController.cs:66)
    MapController.Update () (at Assets/Scrips/Map/MapController.cs:32)

    The segment is this one:

    if(!Physics2D.OverlapCircle(currentChunk.transform.Find("Right up").position, checkerRadius, terrainMask)){

    This ones are after I put the new lines that you give me, but nothing happened

    Here is the complete CODE for MapController:

    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using JetBrains.Annotations;
    using UnityEngine;
    

    public class MapController : MonoBehaviour { public List<GameObject> terrainChunks; public GameObject player; public float checkerRadius; Vector3 noTerrainPosition; public LayerMask terrainMask; public GameObject currentChunk; PlayerMovement pm; [Header("Optimization")] public List<GameObject> spawnedChunks; public GameObject latestChunk; public float maxOpDist; float opDist; float optimizerCooldown; public float optimizerCooldownDur;

    void Start()
    {
        pm=FindObjectOfType<PlayerMovement>();
    
    }
    
    // Update is called once per frame
    void Update(){
    ChunkChecker();
    ChunkOptimizer();
    
    }
    void ChunkChecker(){
        if (!currentChunk){
            return;
        }
    
        if(pm.moveDir.x>0 && pm.moveDir.y==0){ //derecha
            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){ //izquierda
            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){ //Arriba
            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){ //Abajo
            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){ //Derecha arriba
            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){ //Derecha abajo
            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){ //Izquierda arriba
            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){ //Izquierda abajo
            if(!Physics2D.OverlapCircle(currentChunk.transform.Find("Left down ").position, checkerRadius, terrainMask)){
        print(currentChunk);//extra 
        print(currentChunk.transform.Find("Left down"));//extra
                noTerrainPosition=currentChunk.transform.Find("Left down").position;
                SpawnChunk();
            }
        }
    
    
    }
    
    void SpawnChunk(){
        int rand=Random.Range(0, terrainChunks.Count);
        latestChunk=Instantiate(terrainChunks[rand], noTerrainPosition, Quaternion.identity);
        spawnedChunks.Add(latestChunk);
    }
    void ChunkOptimizer(){
        optimizerCooldown-=Time.deltaTime;
        if(optimizerCooldown<= 0f){
            optimizerCooldown=optimizerCooldownDur;
        }
        else{
            return;
        }
        foreach(GameObject chunk in spawnedChunks){
            opDist=Vector3.Distance(player.transform.position, chunk.transform.position);
            if (opDist> maxOpDist){
                chunk.SetActive(false);
            }
            else{
                chunk.SetActive(true);
            }
        }
    
    }

    }

    Thank you a lot!

    #15756
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Max, let’s update the print message for both lines so that we can more clearly identify the issue:

    print("currentChunk variable: " + currentChunk);
    print("currentChunk transform variable: " + currentChunk.transform.Find("Left down"));
    noTerrainPosition=currentChunk.transform.Find("Left down").position;

    Create the issue on your Editor again and look out for the currentChunk variable and currentChunk transform variable messages in your Console.

    #15766
    Max Casas
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    Hi! Here are the messages with the new lines:

    currentChunk variable: TerrainChunk (UnityEngine.GameObject)
    UnityEngine.MonoBehaviour:print (object)
    MapController:ChunkChecker () (at Assets/Scripts/Map/MapController.cs:85)
    MapController:Update () (at Assets/Scripts/Map/MapController.cs:32)
    currentChunk transform variable: 
    UnityEngine.MonoBehaviour:print (object)
    MapController:ChunkChecker () (at Assets/Scripts/Map/MapController.cs:86)
    MapController:Update () (at Assets/Scripts/Map/MapController.cs:32)
    NullReferenceException: Object reference not set to an instance of an object
    MapController.ChunkChecker () (at Assets/Scripts/Map/MapController.cs:87)
    MapController.Update () (at Assets/Scripts/Map/MapController.cs:32)
    #15767
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Max, this chunk in the Console is saying that currentChunk.transform.Find("Left down") is null.

    currentChunk transform variable: 
    UnityEngine.MonoBehaviour:print (object)
    MapController:ChunkChecker () (at Assets/Scripts/Map/MapController.cs:86)
    MapController:Update () (at Assets/Scripts/Map/MapController.cs:32)

    Can you check in all of your chunk prefabs whether the “Left down” GameObject is there, and it is named appropriately? It must be named “Left down”, and not “Left Down” or any other capitalisation pattern.

    #15782
    Max Casas
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    Hi! Sorry for being late, I’m on finals and this is taking a lot of time, and besides I need to finish this game:(
    Thank you so much for your attention
    I’m totally sure that they have the same names
    Look, here are the screenshots with the names:

    View post on imgur.com

    Thanks!

    #15783
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Max, if time is an issue, you can download our project files and replace the art with your own. The project files for the earlier parts are free.

    Alternately, you can share your project files with me over Google Drive and I’ll have a look.

    Check out this post if you are not sure of how to package your project.

    How to package and transfer your Unity project from one device to another

    #15788
    Max Casas
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    Hi!
    I will share the project, If you can take a look, it would be awesome for me, thank you for the attention
    I continued with the game and now Im in part 5, everything is fine (except for the chunks) and focusing on part 5, when I put the health of the player in the Scriptable Object (Knife character, 100 hp), the health doesn’t appear when I run and Debbug the player, so I can’t recibe damage, if you could check it out to
    I’m so sorry for the inconveniences, you are helping me a lot

    https://drive.google.com/drive/folders/1fspOnV_8XzGQZw5ioavG-6dC337Nfr6v

    Please, let me know if the link appears

    #15790
    Max Casas
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    File

    #15791
    Max Casas
    Level 4
    Participant
    Helpful?
    Up
    1
    ::

    I just noticed that in the player stats the void Awake was written as “awake”, I just changed the letter hahaha, and now it works fine!

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

Go to Login Page →


Advertisement below: