• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by surya91 · Feb 24, 2016 at 12:57 AM · terrainarraytreetree creatortree-painting

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.

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

47 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i make individual leaves of a tree glow? 0 Answers

Can't place trees with the tree brush - Tried everything 0 Answers

Why can I not place trees properly in the unity terrain editor? [Unity 5.3.5f1] 0 Answers

Tree billboard on a terrain doesn't update rotation, but the same tree billboard like a gameobject does 0 Answers

Terrain paint tree only places one tree in the middle of circle no matter the density. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges