- Home /
Swapping 2 different tree models
So, I'm trying to swap the first model of a tree with a different one. I would like to do this at a specific time using
public float time; public TimeSpan currenttime; public int days;
I want the tree models to switch every 45ish days. I'm trying to use this to incorporate season to my game. But for now I just want to find a working code for swapping trees/gameobjects. Really hope someone could give me a lead on this! I don't need you to make a code I just need to know how to do it! Thanks in advance!
Answer by James_BadAlchemy · Nov 02, 2018 at 06:28 AM
If you have multiple components accessing something like a universal/global time, I think it would be a good Idea to have a SeasonChanger Script. In this script you use Time.deltaTime to calculate how many days have elapsed. Your calculations would come out to things like int DayNumber, int SeasonNumber, etc.
Then on your tree prefab, you can reference your SeasonChanger script and change the model based on that. I find that it is easier to have two gameObject children on that tree prefab then disable one and enable the other to change the tree models. Good luck!
Answer by dan_wipf · Nov 02, 2018 at 12:29 PM
Ok here is a little Script if you use TerrainComponent.
Just be sure that the Lists of Seasonal Trees contain the Same Amount of Trees
EDIT adjusted the Code to four Seasons + Explanation.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[ExecuteInEditMode]
public class SeasonalScript : MonoBehaviour
{
// Start is called before the first frame update
public int DaysGone = 45;
int SeasonalCounter = 0;
public float Speed = 1;
public float timer = 0;
public bool LoadTrees = false;
bool isOverDaysGone;
public GameObject[] SeasonalTrees_1 = new GameObject[0];
public GameObject[] SeasonalTrees_2 = new GameObject[0];
public GameObject[] SeasonalTrees_3 = new GameObject[0];
public GameObject[] SeasonalTrees_4 = new GameObject[0];
List<Action> SeasonVoids = new List<Action>();
List<TreePrototype> TreeProtottypes = new List<TreePrototype>();
TerrainData _ted;
Terrain _ter;
void OnEnable(){
SeasonVoids.Clear();
SeasonVoids.Add(SeasonOne);
SeasonVoids.Add(SeasonTwo);
SeasonVoids.Add(SeasonThree);
SeasonVoids.Add(SeasonFour);
_ter = GetComponent<Terrain>();
_ted = _ter.terrainData;
timer = 0;
isOverDaysGone = false;
}
// Update is called once per frame
void Update()
{
if(LoadTrees){
SeasonalCounter = 0;
SeasonOne();
LoadTrees = false;
}
if(UnityEditor.EditorApplication.isPlaying){
timer += Time.deltaTime * Speed;
if( timer> DaysGone){
isOverDaysGone = true;
timer = 0;
}
if(isOverDaysGone){
SeasonVoids[SeasonalCounter]();
SeasonalCounter++;
if(SeasonalCounter == 4){
SeasonalCounter = 0;
}
isOverDaysGone = false;
}
}
}
public void SeasonOne(){
TreeProtottypes.Clear();
for(int i = 0; i< SeasonalTrees_1.Length; i++){
TreePrototype TP = new TreePrototype();
TP.prefab = SeasonalTrees_1[i];
TreeProtottypes.Add(TP);
}
_ted.treePrototypes = TreeProtottypes.ToArray();
_ted.RefreshPrototypes();
_ter.Flush();
}
public void SeasonTwo(){
TreeProtottypes.Clear();
for(int i = 0; i< SeasonalTrees_2.Length; i++){
TreePrototype TP = new TreePrototype();
TP.prefab = SeasonalTrees_2[i];
TreeProtottypes.Add(TP);
}
_ted.treePrototypes = TreeProtottypes.ToArray();
_ted.RefreshPrototypes();
_ter.Flush();
}
public void SeasonThree(){
TreeProtottypes.Clear();
for(int i = 0; i< SeasonalTrees_3.Length; i++){
TreePrototype TP = new TreePrototype();
TP.prefab = SeasonalTrees_3[i];
TreeProtottypes.Add(TP);
}
_ted.treePrototypes = TreeProtottypes.ToArray();
_ted.RefreshPrototypes();
_ter.Flush();
}
public void SeasonFour(){
TreeProtottypes.Clear();
for(int i = 0; i< SeasonalTrees_4.Length; i++){
TreePrototype TP = new TreePrototype();
TP.prefab = SeasonalTrees_4[i];
TreeProtottypes.Add(TP);
}
_ted.treePrototypes = TreeProtottypes.ToArray();
_ted.RefreshPrototypes();
_ter.Flush();
}
}
Ok, what i've done: I added to the SeasonVoids(an Action List) all Season Voids (SeasonOne, SeasonTwo, SeasonThree, SeasonFour).
Now if the Timer is above the given Value(DaysGone), it triggers the boolean isOverDaysGone. That will execute SeasonOne, we have now a Counter which Seasons will be next called SeasonalCounter.
After a Seasons is Executed, Seasonal Counter will be Added +1 (Operator ++). If The SeasonCounter reaches the fourth Season(SeasonalCounter == 3(Counts from 0 upwards(0,1,2,3))) then the Counter resets to 0 Again.
If you have any more Questions how it's done, feel free to Ask.
Dan
WOW thats a LOT to look through, I will test this later... I will let you know how it works! It looks very well made btw!
@dan_wipf Also, is there anything I have to do to get this script to work? Do I have to tag trees, add this script to the trees, etc?
add the script to your terrain.
then you can drop your variation of trees to the first array and then the other variation of trees to the second array.
hit the bool loadtrees and start painting. how are the trees made? speedtree? tree creator?
I just got some for testing off of the asset store
Your answer
Follow this Question
Related Questions
Script only working on one child? 0 Answers
Turning off realtime shadows for terrain trees 0 Answers
Having bigger trees 1 Answer