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
1
Question by Lebraz · Nov 02, 2018 at 05:46 AM · treesswapping

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!

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
0

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!

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
avatar image
0

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

Comment
Add comment · Show 14 · 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 Lebraz · Nov 02, 2018 at 06:40 PM 0
Share

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!

avatar image Lebraz · Nov 02, 2018 at 06:42 PM 0
Share

@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?

avatar image dan_wipf Lebraz · Nov 02, 2018 at 07:01 PM 0
Share

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?

avatar image Lebraz dan_wipf · Nov 02, 2018 at 07:25 PM 0
Share

I just got some for testing off of the asset store

Show more comments

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

95 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 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 avatar image

Related Questions

Script only working on one child? 0 Answers

Turning off realtime shadows for terrain trees 0 Answers

Having bigger trees 1 Answer

Tree size problem when using terrain tool 0 Answers

Spotlight lights up trees not in view 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