Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Salmjak · Dec 14, 2015 at 01:57 PM · c#memorytreesremove

Remove a known TreeInstance at runtime.

So, I try to implement a "tree cutting"-method in my game. I do this by selecting an area and creating a new instance of a class "Work area". The work area-class contains a list of all the treeinstances in that area. The problem is I cant find a way to remove trees without editing the whole terrainData.Treeinstances-array.

 List<TreeInstance> newTrees = Terrain.activeTerrain.terrainData.treeInstances.ToList();
 newTrees.RemoveAll(x => x.position == currTree.position);
 Terrain.activeTerrain.terrainData.treeInstances = newTrees.ToArray();

Having about 1300 trees this bit of code creates about 150KB of GC allocation. 54.5KB for get_terrainInstances(). 54.5KB for ToList(). 54.4KB for ToArray() (Values from deep profiling). There must be a way to remove a tree I already have the reference to in a better way. I don't have the index, just the reference to the instance.

Splitting up my terrain in smaller pieces is not something I rather do since I would have to change the majority of my code because of it (map generation, pathfinding, terrain editing, etc).

I've found similar problems on google, but there they (mostly) want a solution for an unknown treeinstance (ex. the tree closest to a gameobject) which is not a part of my problem.

EDIT: I tried to simply move the tree, to see if I could hide it away. but currTree.position = new Vector3(0,0,0) didn't do anything or atleast didnt appear to do anything since the tree model was still in the same position.

Comment
Add comment · Show 4
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 RedHedZed · Dec 14, 2015 at 05:23 PM 0
Share

To clarify, you have a list of 1300 trees, and you want to remove a single tree (say tree 482 or something) and then have a list of 1299 trees?

avatar image Salmjak RedHedZed · Dec 14, 2015 at 05:40 PM 0
Share

Yes, exactly :) And I have tree 482 refered to in a variable (currTree refers to a TreeInstance). But I do not know the index of said tree. So I can't say "RemoveAt()" (which is why I use a lambda expression). The real problem when editing treeinstances is that I have to copy a list twice (ToList() and ToArray()) which allocates a lot of memory. So I know exactly which treeinstance I want to remove, but I still have to iterate over the whole list with a lambda expression.

avatar image RedHedZed Salmjak · Dec 14, 2015 at 07:06 PM 0
Share

Would simply using Remove() rather than RemoveAt() work?

Show more comments

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.

Follow this Question

Answers Answers and Comments

35 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

how do I remove a class item from a list? 1 Answer

Removing a class object from a list(.remove not working) C# 1 Answer

Unknown remaining objects after removing and destroying from list and parent game objects 0 Answers


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