Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by rpettefar · Feb 15, 2014 at 12:44 AM · terraintreetrees

Clearing the TreeInstance array removes all trees perminantly

Hey guys. I wanted to use the terrain tree painting tools to create nice woods. However the terrain trees don't offer all I want so I have an initializer that runs through the TreeInstance array and creates full on GameObject trees. However, when I set the TreeInstance array to an empty array at run time- to get rid of the terrain trees, this removes the trees from the designer too.

I'll include the script that is attached to the terrain.

 public class TerrainInitaliser : MonoBehaviour 
 { 
     public ArrayList treeArray;
     
     // Use this for initialization
     void Start () 
     {
         // Grab the tree array from the terrain
         treeArray = new ArrayList(Terrain.activeTerrain.terrainData.treeInstances);
         int originalCount = treeArray.Count -1;
         Debug.Log("Tree instances: " + originalCount);
         
         // Substitute all the trees for game objects
         for(int i = 0; i <= originalCount; i++)
         {
             GameObject newTree = null;
             
             switch(((TreeInstance)treeArray[0]).prototypeIndex)
             {
                 case 0 : 
                 {
                     newTree = GameObject.Instantiate(Resources.Load("PoC Prefabs/OakTree")) as GameObject;
                 }; break; 
                 
                 case 1 : 
                 {
                     newTree = GameObject.Instantiate(Resources.Load("PoC Prefabs/PalmTree")) as GameObject;
                 }; break; 
                 
                 default : 
                 {
                     return;
                 }
             }
             
             // Set the properties
             newTree.transform.position = new Vector3(
                 Terrain.activeTerrain.terrainData.size.x * ((TreeInstance)treeArray[0]).position.x, 
                 Terrain.activeTerrain.terrainData.size.y * ((TreeInstance)treeArray[0]).position.y, 
                 Terrain.activeTerrain.terrainData.size.z * ((TreeInstance)treeArray[0]).position.z
             );
             
             newTree.transform.localScale = new Vector3(
                 ((TreeInstance)treeArray[0]).widthScale, 
                 ((TreeInstance)treeArray[0]).heightScale, 
                 ((TreeInstance)treeArray[0]).widthScale
             );
             
             // Remove this tree
             treeArray.RemoveAt(0);
         }
         
         // Remove the built-in trees from the terrain
         TreeInstance[] tmpArray = new TreeInstance[0];
         treeArray.CopyTo(tmpArray);
         Terrain.activeTerrain.terrainData.treeInstances = tmpArray;
         
         // Refresh the terrain
         float[,] heights = Terrain.activeTerrain.terrainData.GetHeights(0, 0, 0, 0);
         Terrain.activeTerrain.terrainData.SetHeights(0, 0, heights);
         
         Debug.Log("Done");
     }
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by getyour411 · Feb 15, 2014 at 12:45 AM

That is correct.

Comment
Add comment · Show 4 · Share
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
avatar image rpettefar · Feb 15, 2014 at 12:54 AM 0
Share

Well why is this? I thought changes made during run-time only exist during run-time. Is there a better way of doing what I am currently doing?

avatar image getyour411 · Feb 15, 2014 at 12:56 AM 0
Share

TerrainData is different. If you hit play and then in another docked Scene view modify some terrain (hills, smooth, whatever) and then stop playing, it doesn't go back it's a one way train. There's no good solution I can think of short of writing the contents of the original tree/terraindata out to filesystem because keeping it in memory alone won't suffice, then you'd need to read it back in on Play. Difficult.

avatar image rpettefar · Feb 15, 2014 at 01:02 AM 0
Share

Damn. Fair enough. And I thought using the built-in tree system was going to make life easy! I guess I am going to have to write my own tree populator tool or something. I'm planning on making maps with quite a number of trees so planting them by hand would be pretty intensive.

avatar image getyour411 · Feb 15, 2014 at 01:05 AM 0
Share

I'm in the same boat, which is why I always looking at Tree questions to see if other solutions are offered, there may be an alternative I'm not aware of. Also, by Difficult I didn't mean Impossible - there exists tools in the Unity wiki like PlayerPrefs2 and others that help write array contents to disk, I just haven't tackled it yet.

avatar image
1

Answer by Remingsworth · Dec 16, 2015 at 08:39 AM

The terrainData is being modified at runtime and does not realize it is supposed to change back to it's unaltered state. You have to tell the terrainData to reset at runtime exit. You can fix by adding a script to your terrain.

 public class TerrainScript : MonoBehaviour {
 
     private Terrain terrain;
     public TreeInstance[] originalTrees;
 
     void Start () {
         terrain = GetComponent<Terrain> ();
         //save original trees
         originalTrees = terrain.terrainData.treeInstances;
     }
 
     void OnApplicationQuit() {
         // restore original trees
         terrain.terrainData.treeInstances = originalTrees;
     }
 }
Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Trees not lighting properly 0 Answers

Trees on slopes are floating 1 Answer

Tree painter problem 1 Answer

How do I get a speedtree color? 0 Answers

Billboarded Trees won't update lightning 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