How can I add a seasonal flare to my trees?

  • I would like to add a bit of seasonal flare to my trees added via the Terrain paint widget. The trees are not placed as GameObjects in the hierarchy.
There is not a function to swap between tree prefabs.
Unity Terrain exposes the Tree instances using the array Terrain.treeInstances. Every Tree instance included in this array has a property that stores the tree Prefab configured in the Terrain settings (prototypeIndex).
The tree Prefab is updated automatically by changing this value.
TreeInstance[] currentTreeList;
TerrainData terrain;
int springTreeIndex;
int fallTreeIndex;
bool season;

void ChangeSeasons ()
    // The currentTreeList array must be at least terrain.treeInstances.Length in size
System.Array.Copy (terrain.treeInstances, currentTreeList, terrain.treeInstances.Length);
    if (terrain.treeInstances.Length == currentTreeList.Length)
for (int tcnt=0; tcnt < currentTreeList.Length; tcnt++)
            if (season)
         if (currentTreeList [tcnt].prototypeIndex == springTreeIndex)
            currentTreeList [tcnt].prototypeIndex = fallTreeIndex;
         if (currentTreeList [tcnt].prototypeIndex == fallTreeIndex)
            currentTreeList [tcnt].prototypeIndex = springTreeIndex;
    terrain.treeInstances = currentTreeList;
     season = !season;
More Information
Was this article helpful?
1 out of 1 found this helpful
Have more questions? Submit a request


Please sign in to leave a comment.