Need help! Taking over someones project, need help to understand and where to go from here.

Okay so i am new to posting here, but i am currently working on a VR project where in the scene the leaves on the trees need to shrink and disappear and the water level needs to drop as well. the idea is to just play out a scene in VR whilst the person watches. Currently nothing happens. I’ve attached the main script let me know if you need anything else thanks.

using UnityEngine;
using System.Collections;
using System;

public class DecayBehavior : MonoBehaviour {

    [Header("Config")]
    public float Decay;

    [Header("Hud")]
    public GameObject WaterLevelIndicatorObj;

    [Header("Geo Groups:")]
    public GameObject Grp_TreesAObj;
    public GameObject Grp_TreesBObj;
    public GameObject Grp_TreesCObj;
    public GameObject Grp_GrassAObj;
    public GameObject Grp_GrassBObj;
    public GameObject Grp_GrassCObj;
    public GameObject Grp_BushesAObj;
    public GameObject Grp_BushesBObj;
    public GameObject Grp_BushesCObj;
    public GameObject Grp_GroundObj;

    [Header("Water:")]
    public GameObject Grp_WaterObj;
    Vector3 WaterPosition;
    public float Water_Low;
    public float Water_High;

    [Header("Driven keys properties:")]
    public Animator DrivenKeysAnimator;
    public GameObject WaterDriverObj;
    public GameObject TreesADriverObj;
    public GameObject TreesAColorDriverObj;
    public GameObject TreesBDriverObj;
    public GameObject TreesBColorDriverObj;
    public GameObject TreesCDriverObj;
    public GameObject TreesCColorDriverObj;
    public GameObject GrassADriverObj;
    public GameObject GrassAColorDriverObj;
    public GameObject GrassBDriverObj;
    public GameObject GrassBColorDriverObj;
    public GameObject GrassCDriverObj;
    public GameObject GrassCColorDriverObj;
    public GameObject BushesADriverObj;
    public GameObject BushesAColorDriverObj;
    public GameObject BushesBDriverObj;
    public GameObject BushesBColorDriverObj;
    public GameObject BushesCDriverObj;
    public GameObject BushesCColorDriverObj;
    public GameObject GroundCrossFadeDriverObj;

    [Header("Fx properties:")]
    public GameObject DustObj;
    bool DustFlag = false;

    Renderer rend;
    Color Col;

   // int Frame = 0;
   // bool HalfFpsFlag = false;

    ParticleSystem DustSystem;
    ParticleSystem.EmissionModule DustModule;

    // ----------------------------------
    void Start() {
        DustSystem = DustObj.GetComponent<ParticleSystem>();
        DustModule = DustSystem.emission;

        rend = Grp_GroundObj.GetComponent<Renderer>();
        rend.material.SetFloat("_CrossFade", 0);

        WaterLevelIndicatorObj.transform.localPosition = new Vector3(0, (-Decay + 0.5f)*5f, 0);
    }

    // ----------------------------------
    void Update() {


        UpdateDrivenKeysAnimator();

       /*
        if (HalfFpsFlag)
        {
            Frame++;
            Application.CaptureScreenshot("Screenshot"+Frame.ToString()+".jpg");
        }
        HalfFpsFlag = !HalfFpsFlag;
        */

    }

    // ----------------------------------
    void LateUpdate()
    {
    }

    // ----------------------------------
    void UpdateDrivenKeysAnimator()
    {
        if (Decay < 0) Decay = 0;
        if (Decay >= 1f) Decay = 1f;

        DrivenKeysAnimator.Play("DrivenKeys", 0, Decay);
    }

    // ----------------------------------
    void UpdateWaterLevel()
    {
        WaterPosition = Grp_WaterObj.transform.localPosition;
        WaterPosition.y = Water_Low + (WaterDriverObj.transform.localPosition.y * ( Mathf.Abs(Water_Low-Water_High)  ) );
        Grp_WaterObj.transform.localPosition = WaterPosition;

        rend = Grp_GroundObj.GetComponent<Renderer>();
        rend.material.SetFloat("_CrossFade", GroundCrossFadeDriverObj.transform.localPosition.y);
    }

    // ----------------------------------
    void UpdateTrees()
    {
        UpdateTreeMaterials(Grp_TreesAObj.transform, 1 - TreesADriverObj.transform.localPosition.y, TreesAColorDriverObj.transform.localPosition.y);
        UpdateTreeMaterials(Grp_TreesBObj.transform, 1 - TreesBDriverObj.transform.localPosition.y, TreesBColorDriverObj.transform.localPosition.y);
        UpdateTreeMaterials(Grp_TreesCObj.transform, 1 - TreesCDriverObj.transform.localPosition.y, TreesCColorDriverObj.transform.localPosition.y);
    }

    // ----------------------------------
    void UpdateGrasses()
    {
        UpdateGrassMaterials(Grp_GrassAObj.transform, 1 - GrassADriverObj.transform.localPosition.y, GrassAColorDriverObj.transform.localPosition.y);
        UpdateGrassMaterials(Grp_GrassBObj.transform, 1 - GrassBDriverObj.transform.localPosition.y, GrassBColorDriverObj.transform.localPosition.y);
        UpdateGrassMaterials(Grp_GrassCObj.transform, 1 - GrassCDriverObj.transform.localPosition.y, GrassCColorDriverObj.transform.localPosition.y);
    }

    // ----------------------------------
    void UpdateBushes()
    {
        UpdateTreeMaterials(Grp_BushesAObj.transform, 1 - BushesADriverObj.transform.localPosition.y, BushesAColorDriverObj.transform.localPosition.y);
        UpdateTreeMaterials(Grp_BushesBObj.transform, 1 - BushesBDriverObj.transform.localPosition.y, BushesBColorDriverObj.transform.localPosition.y);
        UpdateTreeMaterials(Grp_BushesCObj.transform, 1 - BushesCDriverObj.transform.localPosition.y, BushesCColorDriverObj.transform.localPosition.y);
    }

    // ----------------------------------
    void UpdateTreeMaterials(Transform Parent, float AlphaValue, float MainColor)
    {
        foreach (Transform U in Parent)
        {
            foreach (Transform T in U)
            {
                rend = T.GetComponent<Renderer>();
                if (rend != null)
                {
                    string[] splitString = rend.material.name.Split(new string[] { "_" }, StringSplitOptions.None);
                        for (int i = 0; i < rend.materials.Length; i++)
                        {
                            if (splitString[0] == "Leaves" || splitString[0] != "FacingLeaves")
                            {
                                Color c = rend.materials[i].color;
                                Col.r = MainColor;
                                Col.g = MainColor;
                                Col.b = MainColor;
                                Col.a = AlphaValue;
                                rend.sharedMaterials[i].color = Col;
                            }
                            else{
                                Col.r = MainColor;
                                Col.g = MainColor;
                                Col.b = MainColor;
                                rend.sharedMaterials[i].color = Col;
                            }
                    }
                }
            }
        }
    }

    // ----------------------------------
    void UpdateGrassMaterials(Transform Parent, float AlphaValue, float MainColor)
    {
        foreach (Transform U in Parent)
        {
            foreach (Transform T in U)
            {
                rend = T.GetComponent<Renderer>();
                if (rend != null)
                {
                        for (int i = 0; i < rend.materials.Length; i++)
                        {
                        Color c = rend.materials[i].color;
                        Col.r = MainColor;
                        Col.g = MainColor;
                        Col.b = MainColor;
                        Col.a = AlphaValue;
                        rend.sharedMaterials[i].color = Col;
                        }
                }
            }
        }
    }

    // ----------------------------------
    public void SetDecay(float Value)
    {
        Decay = Value;

        if (Decay < 0) Decay = 0;
        if (Decay >= 1f) Decay = 0.99f;

        if (Decay > 0.1f && DustFlag == false)
        {
            DustFlag = true;
            DustModule.rateOverTime = 10;
        }
        if (Decay < 0.9f && DustFlag == true)
        {
            DustFlag = false;
            DustModule.rateOverTime = 0;
        }

        RefreshAssets();

        Resources.UnloadUnusedAssets();
    }

    // ----------------------------------
    void RefreshAssets()
    {
        WaterLevelIndicatorObj.transform.localPosition = new Vector3(0, (-Decay + 0.5f) * 5f, 0);

        UpdateWaterLevel();
        UpdateTrees();
        UpdateGrasses();
        UpdateBushes();
    }

    // ----------------------------------
    public float GetDecay()
    {
        return Decay;
    }


}

3274419–253142–DecayBehavior.cs (8.18 KB)

Firstly, use code tags on the forum for proper formatting. Second, you haven’t actually asked a question.

Okay, the main issue im having is i need to change the color of the leaves and i need to make them shrink (to make the trees look like they’re dying) this code is “supposed” to do that but does not. the way i was thinking of doing it was to increase the alpha cutoff on the texture and also change the color there as well. but i am unsure on how to do this. I suppose my question would be what is the best way to go about doing that? Sorry this was my first post so i don’t know how to use it properly yet.

Well first thing first, it doesn’t do anything because nothing calls those UpdateXXX methods.

Using code tags properly Code tags…your best friend.

Also very scared by the fact whoever wrote this code didn’t use collections…

1 Like

Thanks for the link stops me from being a noob. And I know the other scripts are of the same standard as well.