Advertisement
EmmyDev

Modular Tree

Jul 2nd, 2023
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ModularTree : MonoBehaviour
  6. {
  7.     public SpriteRenderer[] treeParts;
  8.     [SerializeField] private Sprite[] rottenParts;
  9.     [SerializeField] private Sprite[] healthyParts;
  10.     [SerializeField] private Sprite babyTreeSprite;
  11.     public bool[] isHealthy;
  12.     public int growthStage;
  13.  
  14.     private void Update()
  15.     {
  16.         ChangeTree();
  17.     }
  18.  
  19.     public void ChangeTree()
  20.     {
  21.         for(int i = 0; i <= growthStage; i++)
  22.         {
  23.             //Checks for each branch
  24.             if(isHealthy[i])
  25.             {
  26.                 //baby con
  27.                 if(growthStage == 0)
  28.                 {
  29.                     treeParts[i].sprite = babyTreeSprite;
  30.                     ClearTreeBranches();
  31.                 }
  32.                 else
  33.                 {
  34.                     treeParts[i].sprite = healthyParts[i];
  35.                 }
  36.                
  37.             }
  38.             else if(!isHealthy[i])
  39.             {
  40.                 treeParts[i].sprite = rottenParts[i];
  41.             }
  42.  
  43.         }
  44.     }
  45.     public void ClearTreeBranches()
  46.     {
  47.         for(int i = 1; i < treeParts.Length; i++)
  48.         {
  49.             treeParts[i].sprite = null;
  50.         }
  51.     }
  52.  
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement