2D Caves game lag

Hello!
At first sorry for my English.

I’m writing 2D top-down caves game inspired by Minecraft. I randomly generate world like in these article: Generate Random Cave Levels Using Cellular Automata | Envato Tuts+.

This is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GenerateMap : MonoBehaviour {

    [SerializeField] GameObject m_StoneBrick;        //2d Sprite 16 by 16 px with box colider
    private GameObject m_StonesConteiner;

    [SerializeField] int m_Width = 51;
    [SerializeField] int m_Height = 51;
    [SerializeField] int m_ChanceToSpawnBrick = 50;
    [SerializeField] int m_Loop = 10;

    bool [,]m_BitMap;

    void Start () {
        m_StonesConteiner = new GameObject ("StonesContainer");
        m_BitMap = new bool [m_Width, m_Height];


        //Generate
        int m_Random;
        for (int i = 0; i < m_Width; i++) {
            for (int j = 0; j < m_Height; j++) {
                m_Random = Random.Range (0, 100);
                if (m_ChanceToSpawnBrick < m_Random) {
                    m_BitMap [i, j] = false;
                } else {
                    m_BitMap [i, j] = true;
                }
            }
        }
        for (int i = 0; i < m_Loop; i++) {
            symulate ();
        }

        printMap();
    }

    void symulate(){
        bool[,] copy = new bool[m_Width, m_Height];
        int nbs;
        for (int i = 0; i < m_Width; i++) {
            for (int j = 0; j < m_Height; j++) {
                nbs = add (i, j);
                if (m_BitMap [i, j]) {
                    if (nbs < 4) {
                        copy [i, j] = false;
                    } else {
                        copy [i, j] = true;
                    }
                } else {
                    if (nbs > 4) {
                        copy [i, j] = true;
                    } else {
                        copy [i, j] = false;
                    }
                }
            }
        }

        for (int i = 0; i < m_Width; i++) {
            for (int j = 0; j < m_Height; j++) {
                m_BitMap [i, j] = copy [i, j];
            }
        }
    }

    int add(int x, int y){
        int nbs = 0;
        for (int i = -1; i < 2; i++) {
            for (int j = -1; j < 2; j++) {
                if((i != 0) || (j != 0)){
                    if((x + i > 0) && (x + i < m_Width) && (y + j > 0) && (y + j < m_Width)){
                        if (m_BitMap [x + i, y + j]) {
                            nbs++;
                        }
                    } else {
                        nbs++;
                    }
                }
            }
        }
        return nbs;
    }

    void printMap(){
        for (int i = 0; i < m_Width; i++) {
            for (int j = 0; j < m_Height; j++) {
                if (m_BitMap [i, j]) {
                    putStone (i, j);
                }
            }
        }
    }

    void putStone(float x, float y){
        Instantiate(m_StoneBrick, new Vector3(x, y, 0), Quaternion.identity, m_StonesConteiner.transform);
    }
}

End i made very simple script to move camera when i user press arrow key.
After starting with width and height mor than 50 unity started to lag. With width and height equals to 50 game work but not very fluently. What am I doing wrong? Is there any possibility to run it fluently?

The first thing that I think of is that my PC is to weak. But i don’t think so. It’s very simple game and my laptop is good enaugh for game like minecraft.

Thanks for any help :slight_smile:

I only skimmed through your code but it seems everything is generated at start, so you’re not doing anything on the update? If that’s the case the problem has to be related with rendering these squares. Do they have other components on them?

Yes. There is only one thing in the update function in camera script.

    void Update () {
        if (Input.GetKey (KeyCode.LeftArrow)) {
            transform.position = new Vector3(transform.position.x - 0.1f, transform.position.y, transform.position.z);
        }

        if (Input.GetKey (KeyCode.RightArrow)) {
            transform.position = new Vector3(transform.position.x + 0.1f, transform.position.y, transform.position.z);
        }

        if (Input.GetKey (KeyCode.UpArrow)) {
            transform.position= new Vector3(transform.position.x, transform.position.y + 0.1f, transform.position.z);
        }

        if (Input.GetKey (KeyCode.DownArrow)) {
            transform.position = new Vector3(transform.position.x, transform.position.y - 0.1f, transform.position.z);
        }
    }

No. I have only transform, rigidbody, boxColider and render components.

Ok. I found causes of lag. I have about 5000 objects and colliders. And in each frame computer must check collision for all objects. Game work fluently when i turn off all colliders.