This script crashes each time it is run, this only started occurring after I added a check to not create new tiles over ones that already were created. I hope anyone knows what’s going on.
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class GenerateEnviroment : MonoBehaviour
{
public float curX = 5f;
public float curZ = 5f;
float xQuad;
float zQuad;
public float curWithinX = 0f;
public float curWithinZ = 0f;
public float curTileX;
public float curTileZ;
bool cando = true;
float mult = 1f;
public List<List<int>> createdTiles = new List<List<int>>();
void Start() {
createdTiles.Add( new List<int>{0, 0} );
}
// Update is called once per frame
void Update() {
// Get Position
curX = transform.position.x + 5;
curZ = transform.position.z + 5;
// Current Tile and Where
curTileX = Mathf.Floor(curX / 10f)*10f;
curWithinX = curX + (Mathf.Floor(curX / 10f)*10f);
curTileZ = Mathf.Floor(curZ / 10f)*10f;
curWithinZ = curZ + (Mathf.Floor(curZ / 10f)*10f);
// Spacebar
if (Input.GetKey(KeyCode.Space) && cando == true) {
cando = false;
for (int xCycle = -1; xCycle < 2; xCycle++) {
for (int zCycle = -1; zCycle < 2; zCycle++) {
List<int> currentTiles = new List<int>{xCycle, zCycle};
for (int b = 0; b < createdTiles.Count; b++) {
Debug.Log("point");
if (createdTiles[b] != currentTiles) {
Debug.Log("create");
GameObject tile = Instantiate(GameObject.Find("Prefabs/GroundTile"), new Vector3((curTileX*mult) + xCycle*10f, 0, (curTileZ*mult) + zCycle*10f), Quaternion.identity);
tile.name = "tile" + (xCycle+curX) + "," + (zCycle+curZ);
createdTiles.Add( new List<int>{xCycle, zCycle} );
}
}
}
}
}
if (Input.GetKey(KeyCode.O)) {cando = true;}
}
}