- Home /
I am trying try create chunks in my game but it just makes infinite clones
I am only 13 so my code is gonna be a little rusty but I am trying to break a heightmaps into chunks that can be culled when the player does not see it but when making my script that makes the terrains I just get infinite clones. Any help would be very apreciated!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChunkColumns : MonoBehaviour {
string num;
public bool apply;
public int LengthHorizontally;
public int LengthVertically ;
public int TileResolution;
public int VisionRadius;
int terrainSizeX;
int terrainSizeY;
public GameObject Player;
public GameObject Column;
// Use this for initialization
void Start()
{
num = this.gameObject.name;
char[] Num = num.ToCharArray();
Apply();
}
void Apply () {
terrainSizeX = LengthHorizontally % TileResolution;
terrainSizeY = LengthVertically % TileResolution;
Vector3 Position = this.transform.position;
Position.x = Position.x + terrainSizeX;
Position.y = Position.y + terrainSizeY;
Quaternion NormRot = this.transform.rotation;
if (transform.position.x < LengthHorizontally) {
GameObject ColumnNum = Instantiate(Column, Position, NormRot) as GameObject;
}
}
// Update is called once per frame
void Update () {
}
}
I'm not sure it is relevant to what you want, but you could check out this tutorial, it has some good ideas and very good coding practices: https://catlikecoding.com/unity/tutorials/noise-derivatives/
Answer by SamohtVII · Aug 31, 2018 at 06:05 AM
Where is this script attached. Are you Instantiating a 'Column' which has this script, then it will Start(), enter 'Apply()' Instantiate another column and then that will 'Start(), Apply(), Instantiate()' etc infinitely?
I also have another piece of code not working 100%, with it I am trying to create the vision radius of the terrains that will be rendered, but it only shows the first terrain in the column, here it is: public class Culling : $$anonymous$$onoBehaviour { public int visionRadius; public GameObject player; // Update is called once per frame void Update () { if (transform.position.x - player.transform.position.x > visionRadius || transform.position.y - player.transform.position.y > visionRadius) { GetComponent<Terrain>().enabled = false; GetComponent<TerrainCollider>().enabled = false; } else { GetComponent<Terrain>().enabled = true; GetComponent<TerrainCollider>().enabled = true; } } }
Answer by myzzie · Aug 31, 2018 at 11:20 PM
Looks like you're instantiating a gameobject with this script attached. That would indeed cause an infinite loop
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How to use this code for instantiate? 0 Answers
Multiple Cars not working 1 Answer
What's a better way to code in chunks 1 Answer
How to associate underlying data structure to prefab at runtime? 1 Answer