Procedural generation script only spawning in one kind of tile

I made a script that randomly spawns either land or water and than smooths that out using cellular automata. When I run unity, it only spawns water though. Do any of you have any idea what is causing this problem in my script?

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

public class worldGen : MonoBehaviour
{
    public Tile water;
    public Tile ground;
    int[,] waterCount;
    int[,] groundCount;

    public Tilemap tilemap;
    public int width = 100;
    public int height = 100;

    public Tile[] tileTypes;

    void Start()
    {
        genRandomTiles();
        cellularAutomata();
        smoothTiles();
    }

    void genRandomTiles()
    {
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                int rand = Random.Range(0, tileTypes.Length);
                Vector3Int pos = new Vector3Int(x, y, 0);
                tilemap.SetTile(pos, tileTypes[rand]);
            }
        }
    }
    void cellularAutomata()
    {
        waterCount = new int[width, height];
        groundCount = new int[width, height];

        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                List<TileBase> neighbours = new List<TileBase>();
                for (int i = -1; i < 2; i++)
                {
                    for (int j = -1; j < 2; j++)
                    {
                        int neighbour_x = x + i;
                        int neighbour_y = y + j;

                        if (i == 0 && j == 0)
                        {

                        }
                        else
                        {
                            Vector3Int pos = new Vector3Int(neighbour_x, neighbour_y, 0);
                            neighbours.Add(tilemap.GetTile(pos));
                        }
                    }
                }
                for (int i = 0; i < neighbours.Count; i++)
                {
                    if (neighbours[i] = water)
                    {
                        waterCount[x,y]++;
                    }
                    else if (neighbours[i] = ground)
                    {
                        groundCount[x,y]++;
                    }
                }
            }
        }
    }

    void smoothTiles()
    {
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                Vector3Int pos = new Vector3Int(x, y, 0);
                if(waterCount[x,y] > 4)
                {
                    tilemap.SetTile(pos, water);
                }
                else if (groundCount[x,y] > 4)
                {
                    tilemap.SetTile(pos, ground);
                }
            }
        }
    }
}

Line 68 and 72 there is a bug:

                    if (neighbours[i] = water)
                    {
                        waterCount[x,y]++;
                    }
                    else if (neighbours[i] = ground)
                    {
                        groundCount[x,y]++;
                    }

You are using single equals sign =. This is the ASSIGNMENT operator. Rather than checking for equality, you are actually setting every neighbor to water on this line. You need to use the equality comparison operator: ==

If this still doesn’t do it, check the following:

  • What does your tileTypes array look like?
  • What are water and ground assigned to
  • Add Debug.Log around line 34 and print which kind of tile you are generating. Are you ever generating ground tiles there?
  • Add Debug.Log around line 90 and line 94 whenever you change a tile type. Are all the tiles being changes to water tiles there?
1 Like

thank you so much! this fixed my problem and now my worlds generate. I really should have noticed that I put one equal sign rather than two