Terrain Tree Index Removed, But Sometimes Not And I got An Error
I'm Sorry if my script variablel use my language. i have a few problem with the script which after i remove the tree, i dont know if its bug or not.
first. after chop, the tree gone/removed, but Tree Collider still there, nd when i press forward. it still hold me to move forward. i'm using speed tree Paint and use standard asset unity 5 tree. and there's no capsule collider etc. just activated tree Collider in terrain.
and the script i use to remove the tree Terrain is this.
var lepasPohon = new List<TreeInstance> (terrain.treeInstances);
lepasPohon.RemoveAt(indeksNomorPohon);
terrain.treeInstances = lepasPohon.ToArray ();
do we have an option not using Unity.Collections.Generic but stilll can access the array and remove it?
second : i create new tree using speedTree Creator. it work, the collider uses capsule and tree gone. but sometimes the tree in my face still there . and sometimes another tree which little bit far from (close near the tree i face) removed. maybe my camera or something? and i got error about this if the tree not removed and i try to click/chop more than that.
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index
AND
Should check if Object Null. or Not
the tree wont removed but can instantiate 2 - 3 tree on same index tree if i click more same tree, the error show. i has try solve this 4 days (72 hours) but still cant solve it. i hope someone can help to fix this scripts. i'm still learn about script and try to create survival game. but stuck at tree script. i'm try write it using my language for understand that code.
Note : i have try code .Remove(closestTree) but have same problem. and try ArrayList(terrain.treeInstances) but it wont remove the tree.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TestTreearray : MonoBehaviour {
//karena saya akan berinteraksi dengan pohon, maka saya harus membuat beberapa variabel untuk handel pohon.
//jika kurang, nanti akan kita tambahkan kembali.
public int darahPohon = 5;
public int darahFallPohon = 5;
public int darahAwal = 5;
public int darahFallAwal = 5;
private bool pohonDahTerpotong = false;
public TerrainData terrain;
public TreeInstance[] seluruhPohon;
public TreeInstance Pohon;
public TreeInstance dekatPohon;
public GameObject protoPrefab;
public int indeksNomorPohon;
public int protoIndex;
public int nomorPohonPotong = -1;
public int kecepatan = 50;
public int simpananPohon;
//mengambil data posisi pohon di dunia.
public Vector3 posisiPohon;
public Vector3 posisiPohonDunia;
//public ArrayList lepasPohon;
//Mengambil Data Kamera
public Camera kamera;
public float lensa = 5f;
public GameObject Impact;
public GameObject[] FallingTreePrefab;
private GameObject fallingTree;
public TreeKontrol skrip;
// Use this for initialization
//dapatkan LOG Lumberr!!! ^_^
public Vector3 posisi;
public GameObject Logs;
void Start () {
terrain = Terrain.activeTerrain.terrainData;
seluruhPohon = Terrain.activeTerrain.terrainData.treeInstances;
//lepasPohon = new ArrayList(terrain.treeInstances);
kamera = GameObject.FindObjectOfType<Camera> ();
}
// Update is called once per frame
void Update () {
RaycastHit hit;
var impactOnScreenPosition = Camera.main.WorldToScreenPoint(Impact.transform.position);
Ray ray = Camera.main.ScreenPointToRay(impactOnScreenPosition);
//Ray ray = new Ray (kamera.transform.position, kamera.transform.forward);
if (Physics.Raycast (ray, out hit, lensa)) {
float maksimalJarak = float.MaxValue;
for (int i = 0; i < seluruhPohon.Length; i++) {
Pohon = seluruhPohon [i];
posisiPohonDunia = Vector3.Scale (Pohon.position, terrain.size) + Terrain.activeTerrain.transform.position;
float jarak = Vector3.Distance (posisiPohonDunia, hit.point);
if (jarak < maksimalJarak) {
maksimalJarak = jarak;
indeksNomorPohon = i;
posisiPohon = posisiPohonDunia;
dekatPohon = Pohon;
}
}
protoIndex = dekatPohon.prototypeIndex;
if (Input.GetKeyDown (KeyCode.Mouse0) && pohonDahTerpotong == false) {
darahPohon -= 1;
}
protoPrefab = Terrain.activeTerrain.terrainData.treePrototypes [protoIndex].prefab;
if (darahPohon <= 0) {
//pohonmati (5);
//lepasPohon = new IList<TreeInstance> (terrain.treeInstances);
var lepasPohon = new List<TreeInstance> (terrain.treeInstances);
lepasPohon.RemoveAt(indeksNomorPohon);
terrain.treeInstances = lepasPohon.ToArray ();
//sangat penting :D
float[,] heights = terrain.GetHeights(0, 0, 0, 0);
terrain.SetHeights(0, 0, heights);
// Put a falling tree in its place, tilted slightly away from the player
fallingTree = (GameObject) Instantiate(FallingTreePrefab[protoIndex], posisiPohon,Quaternion.AngleAxis(2, Vector3.right));
fallingTree.transform.localScale = new Vector3(dekatPohon.widthScale * fallingTree.transform.localScale.x,dekatPohon.heightScale * fallingTree.transform.localScale.y,dekatPohon.widthScale * fallingTree.transform.localScale.z);
fallingTree.GetComponent<Rigidbody> ().isKinematic = false;
//fallingTree.GetComponent<Rigidbody> ().AddTorque (kamera.transform.right * kecepatan, ForceMode.Impulse);
fallingTree.GetComponent<Rigidbody> ().AddForce (transform.forward * kecepatan);
darahPohon = darahAwal;
StartCoroutine (Tunggu ());
}
//pastikan bahwa pohon yg kita tebang sama, jika tidak, reset data darah pohon.
if (nomorPohonPotong != indeksNomorPohon) {
darahPohon = darahAwal;
nomorPohonPotong = indeksNomorPohon;
darahFallPohon = darahFallAwal;
pohonDahTerpotong = false;
}
if (Input.GetKeyDown (KeyCode.Mouse0) && pohonDahTerpotong == true) {
darahFallPohon -= 1;
}
if (darahFallPohon <= 0) {
GameObject.Destroy(fallingTree);
posisi = new Vector3 (Random.Range (-1.0f, 1.0f), 0, Random.Range (-1.0f, 1.0f));
Instantiate (Logs, fallingTree.transform.position + new Vector3 (0, 0, 0) + posisi, Quaternion.identity);
Instantiate (Logs, fallingTree.transform.position + new Vector3 (2, 2, 0) + posisi, Quaternion.identity);
Instantiate (Logs, fallingTree.transform.position + new Vector3 (5, 5, 0) + posisi, Quaternion.identity);
darahFallPohon = darahFallAwal;
pohonDahTerpotong = false;
}
}
}
IEnumerator Tunggu() {
yield return new WaitForSeconds (7);
fallingTree.GetComponent<Rigidbody> ().isKinematic = true;
pohonDahTerpotong = true;
}
void OnApplicationQuit() {
Terrain.activeTerrain.terrainData.treeInstances = seluruhPohon;
}
/*private void pohonmati(int o) {
lepasPohon.RemoveAt (indeksNomorPohon);
lepasPohon.ToArray ();
darahPohon = o;
}*/
}
all opinion will be appreciated. and sorry if i have mistake.
Your answer
Follow this Question
Related Questions
How do i make individual leaves of a tree glow? 0 Answers
OnTriggerEnter does not work on placed terrain trees 0 Answers
Transparrent Trees 0 Answers
Trees like in City Skylines 0 Answers