I recently changed a bit of my code so that when the world is generated the blocks are slightly smaller that before because I didn't like how big they were. Note it used to work when they were this small. But this time when I changed it the game won't launch when I press the play button. I believe that either the WorldGen or Chunk script is the script causing the error but there are no syntax errors and after looking through it multiple times I don't see anything wrong with it.
The Chunk Script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Chunk {
// Ground level chunk Values
public static int size = 16;
public int position;
public Block[,] blocks;
public GameObject[,] blockObjects;
private static float heightModifier = 100f;
private float smoothness = Random.Range(0.05f, 0.075f);
private BlockManager blockManager;
// Cave generation chunk Values
public int height = 128;
public string seed;
public bool useRandomSeed;
[Range(0, 100)]
public int randomFillPercent;
int[,] map;
public Chunk(BlockManager blockManager, int position) {
this.blockManager = blockManager;
this.position = position;
blocks = new Block[size, WorldGen.height];
blockObjects = new GameObject[size, WorldGen.height];
}
public void GenerateBlocks() {
// World generation
for (int x = 0; x < size; x++) {
// Perlin noise setup
float pValue = Mathf.PerlinNoise((position * size + x) * 0.05f, 5f * 0.05f);
float pHeight = Mathf.RoundToInt(pValue * 20f + heightModifier);
// Ground level
for (int y = 0; y < WorldGen.height; y++) {
// Generate the ground
if (y <= pHeight) { // Generate ground
if (y == pHeight - 1) { // Generate grass layer
blocks[x, y] = blockManager.FindBlock(1);
} else if (y == pHeight) { // Generate foliage
//Grass
if (Random.value < 0.7) {
blocks[x, y] = blockManager.FindBlock((byte)Random.Range(3, 7));
}
} else if (y == pHeight - 2 || y == pHeight - 3 || y == pHeight - 4 || y == pHeight - 5 || y == pHeight - 6 || y == pHeight - 7 || y == pHeight - 8 || y == pHeight - 9 || y == pHeight - 10) { // Generate dirt layer
blocks[x, y] = blockManager.FindBlock(2);
} else { // Generate stone layer with resources
float random = Random.Range(0f, 100f);
Block spawnedBlock = blocks[x, y] = blockManager.FindBlock((byte)Random.Range(8, 11));
if (spawnedBlock.chance >= random) {
blocks[x, y] = spawnedBlock;
} else{
blocks[x, y] = blockManager.FindBlock(7);
}
}
} else { // Leave as air block
blocks[x, y] = blockManager.FindBlock(0);
}
}
}
randomBlock();
}
public void randomBlock() {
uint randomONE = (uint)Random.Range(0, size);
uint randomTWO = (uint)Random.Range(0, WorldGen.height);
blocks[randomONE, randomTWO] = blockManager.FindBlock(0);
for (int i = 0; i < 60; i++) {
switch (Random.Range(0, 3)) {
case 0:
for (int p = 2; p >= 0; p++) {
}
if (randomTWO < WorldGen.height) {
blocks[randomONE, randomTWO + 1] = blockManager.FindBlock(0);
randomTWO += 1;
}
break;
case 1:
if (randomTWO > 0) {
blocks[randomONE, randomTWO - 1] = blockManager.FindBlock(0);
randomTWO -= 1;
}
break;
case 2:
if (randomONE < size) {
blocks[randomONE + 1, randomTWO] = blockManager.FindBlock(0);
randomONE += 1;
}
break;
case 3:
if (randomONE > 0) {
blocks[randomONE - 1, randomTWO] = blockManager.FindBlock(0);
randomONE -= 1;
}
break;
default:
break;
}
}
}
// Destroy Method
public void Destroy() {
for ( int x = 0; x < size; x++) {
for (int y = 0; y < WorldGen.height; y++) {
blocks[x, y] = null;
GameObject.Destroy(blockObjects[x, y]);
}
}
}
}
Then the WorldGen script consists of: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class WorldGen : MonoBehaviour {
public GameObject player;
private BlockManager blockManager;
private ItemManager itemManager;
public static int height = 512;
private List chunks;
public int viewDistance = 4;
private void Start() {
blockManager = GameObject.Find("GameManager").GetComponent();
itemManager = GameObject.Find("GameManager").GetComponent();
chunks = new List();
player = SpawnPlayer(7, 65);
}
private void Update() {
int playerChunk = Mathf.FloorToInt(player.transform.position.x / 8);
for (int i = playerChunk - viewDistance; i < playerChunk + viewDistance; i++) {
bool spawn = true;
foreach (Chunk chunk in chunks) {
if (chunk.position == i) {
spawn = false;
}
}
if (spawn) {
// Spawn the chunks
Chunk newChunk = new Chunk(blockManager, i);
newChunk.GenerateBlocks();
SpawnBlocks(newChunk);
chunks.Add(newChunk);
}
}
// Cleanup old chunks
foreach (Chunk chunk in chunks) {
if (chunk.position < playerChunk - viewDistance || chunk.position > playerChunk + viewDistance) {
chunk.Destroy();
chunks.Remove(chunk);
break;
}
}
}
private void SpawnBlocks(Chunk chunk) {
for (int x = 0; x < Chunk.size; x++) {
for (int y = 0; y < height; y++) {
if (chunk.blocks[x, y] != null) {
GameObject blockGO = new GameObject();
SpriteRenderer sr = blockGO.AddComponent();
sr.sprite = chunk.blocks[x, y].sprite;
blockGO.name = chunk.blocks[x, y].displayName;
blockGO.transform.position = new Vector3((chunk.position * Chunk.size) + x, y);
blockGO.tag = "Block";
chunk.blockObjects[x, y] = blockGO;
if (chunk.blocks[x, y].isSolid) {
BoxCollider2D col = blockGO.AddComponent();
}
}
}
}
}
public void UpdateChunk(Chunk chunk) {
for (int x = 0; x < Chunk.size; x++) {
for (int y = 0; y < height; y++) {
if (chunk.blocks[x, y] != null && chunk.blockObjects[x, y] == null) {
// Create block
GameObject blockGO = new GameObject();
SpriteRenderer sr = blockGO.AddComponent();
sr.sprite = chunk.blocks[x, y].sprite;
blockGO.name = chunk.blocks[x, y].displayName;
blockGO.transform.position = new Vector3((chunk.position * Chunk.size) + x, y);
blockGO.tag = "Block";
chunk.blockObjects[x, y] = blockGO;
if (chunk.blocks[x, y].isSolid) {
BoxCollider2D col = blockGO.AddComponent();
}
} else if (chunk.blocks[x, y] == null && chunk.blockObjects[x, y] != null) {
// Destroy block
GameObject.Destroy(chunk.blockObjects[x, y]);
chunk.blockObjects[x, y] = null;
}
}
}
}
public void DestroyBlock(GameObject block) {
Vector3 blockPos = block.transform.position;
Vector2 chunkPos = WorldPosToChunkPos(blockPos.x, blockPos.y);
//Destroy reference
int x = (int) chunkPos.x;
int y = (int) chunkPos.y;
Chunk chunk = ChunkAtPos(blockPos.x);
foreach(Drop drop in chunk.blocks[x, y].drops) {
if (drop.DropChanceSuccess()) {
//Create Drops
GameObject dropObject = new GameObject();
dropObject.transform.position = block.transform.position;
dropObject.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
dropObject.AddComponent().sprite = itemManager.FindItem(drop.itemName).sprite;
dropObject.AddComponent();
dropObject.AddComponent();
dropObject.layer = 9;
dropObject.AddComponent().target = GameObject.FindWithTag("Player").transform;
dropObject.name = drop.itemName;
}
}
//Set the block to an air block
chunk.blocks[x, y] = blockManager.FindBlock(0);
//Destroy gameObject
GameObject.Destroy(block);
}
public void PlaceBlock(Block block, Vector3 pos) {
Chunk chunk = ChunkAtPos(pos.x);
Vector2 chunkPos = WorldPosToChunkPos(pos.x, pos.y);
chunk.blocks[(int) chunkPos.x, (int) chunkPos.y] = block;
UpdateChunk(chunk);
}
private Chunk ChunkAtPos(float x) {
int chunkIndex = Mathf.FloorToInt(x / Chunk.size);
foreach (Chunk chunk in chunks) {
if (chunk.position == chunkIndex) {
return chunk;
}
}
return null;
}
public Vector2 WorldPosToChunkPos(float x, float y) {
int xPos = Mathf.RoundToInt(x - (ChunkAtPos(x).position * Chunk.size));
int yPos = Mathf.RoundToInt(y);
return new Vector2(xPos, yPos);
}
public Vector2 ChunkPosToWorldPos(int x, int y, int chunkPos) {
int xPos = Mathf.FloorToInt(x + (chunkPos * Chunk.size));
int yPos = Mathf.FloorToInt(y);
return new Vector2(xPos, yPos);
}
private GameObject SpawnPlayer(float x, float y) {
GameObject playerObj = GameObject.Instantiate(player, new Vector3(x, y), Quaternion.identity);
return playerObj;
}
}
↧