Problems with infinite BlockPlacements

Hi so I’m new to unity and I’m currently working on making an infinite flat plane made up of blocks using chunk to determine render distance, however this is three main problems with my code that i cant figure out why its happening.

  1. Countless more blocks overlap and spawn on each other than needed
  2. The Loading of the chunks/ blocks don’t spawn around the player however spawn to the side of the player
  3. Due to the amount of extrablocks spawning framerate drops down to about 5 frames

Image of the problem

public class Chunk : MonoBehaviour
{
    public GameObject ChunkManager;
    public GameObject ObjectCord;
    public int chunksize;
    private int CurrentChunkX;
    private int RawX;
    private int CurrentChunkZ;
    private int RawZ;
    [SerializeField] int Maxchunks; // render distance

    // Update is called once per frame
    void Update()
    {
        List<string> LoadedList = new List<string>();
        RawZ = (int)(Mathf.Floor(ObjectCord.transform.position.z)); // rounds down
        RawX = (int)(Mathf.Floor(ObjectCord.transform.position.x));
       
        //checks if RawX or RawZ is not 0, so when divided later errors dont popup
        if (RawZ != 0)
        {
            CurrentChunkZ = (RawZ / chunksize);
        }
        else
        {
            CurrentChunkZ = 1;
        }
        if (RawX != 0)
        {
            CurrentChunkX = (RawX / chunksize);
        }
        else
        {
            CurrentChunkX = 1;
        }
      
       
        for (int p = (CurrentChunkX - (Maxchunks / 2)); p < Maxchunks; p++) // the X value on the 2d chunk loading table, made to be half so it loads around the player not in front
        {
            for (int o = (CurrentChunkZ - (Maxchunks / 2)); o < Maxchunks; o++) // the z value of the chunks
            {
                string temp = ("X:" + p + ", Z:" + o);
                if (!LoadedList.Contains("X:" + p + ", Z:" + o))  //check if its not already added to the loaded list
                {
                    LoadedList.Add(temp); // adds to loadedlist Chunks
                    int LoadChunkX = p * chunksize;
                    int LoadChunkY = o * chunksize;
                    chunkload(LoadChunkX, LoadChunkY); // starts loading in that chunk
                }
                else
                {
                       
                }

            }
        }

    }

    void chunkload(int x, int y)
    {
        // places a block in each block position at each chunk
        for (int PlacerX = 0; PlacerX < chunksize; PlacerX++)
        {
            for (int PlacerY = 0; PlacerY < chunksize; PlacerY++)
            {
                //clones block and fills
                GameObject BoxClone = Instantiate(ChunkManager, new Vector3(x + PlacerX, ChunkManager.transform.position.y, y + PlacerY), ChunkManager.transform.rotation);
                BoxClone.name = "Box" + x + y;
            }
        }
    }
}

Heres is what the flow sheet looks like for both functions

Any input or help would be amazing thank you.

Any reason you don’t use temp on line 43 when you check contains? That’s just needless violation of DRY / SPOT , asking for an error.

Beyond that, get ready to debug… if your “flow sheet” is accurate, then start figuring out how your program deviates from it.

If your program doesn’t deviate from the “flow sheet,” perhaps your “flow sheet” is logically faulty.

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: How To - Capturing Device Logs on iOS or this answer for Android: How To - Capturing Device Logs on Android

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

If you’re just making a minecraft clone, there are like ten billion tutorials out there… the entire process is pretty well understood now 20 years into the minecraft era. :slight_smile:

I don’t know what expectations you have. But only a few people on the planet are able to spot logical/algorithmic errors in complex code just by looking at it. Even moreso when it contains several errors/bugs. And even less people are willing to debug the code for you for free. So Kurt’s advise is correct. Your best option is to debug it yourself. Focus on one problem at a time. Verify your assumptions. See where it goes downhill.
Or, alternatively, recognize that the problem/task is too complex for you and do something simpler first.

And most of your thread tags don’t really make sense (unityscript?).