Tree Generator Material Fade issues

Question : Is there any way to fade out trees created with Unity TreeCreator?

Explanation:

I am using the built-in tree generator, and I want the tree to fade out when my player moves under it –

7557889--934705--ezgif.com-gif-maker (9).gif

– so naturally I tried –

Hitinfo.collider.gameObject.GetComponent<Renderer>().material.color

– and altering the alpha of the color in a variety of ways. No use. So I looked at the material closer, and it uses the Nature> Tree Creator Bark shader which uses a material I created of the Tree Creator Bark and Tree Creator Leaf Fast type. The issue is they don’t seem to support transparency. I have scoured the internet looking, searched the forums, and done so may tutorials I could scream. No one seems to have an answer or even submitted the issue before. The closest I found was a forum pertaining to the same issue within Speed Tree and the answer seemed to be a resounding No, until a user posted a custom shader modification. I am not versed in shaders, and was wondering if anyone has any direction?

I also tried switching out the materials at Runtime, which got me close –

7557889--934723--ezgif.com-gif-maker (10).gif

– However only the bark material is changed, and when I change from -

obstacle.GetComponent<Renderer>().material = fadeMat;

– to –

            obstacle.GetComponent<Renderer>().materials[0] = fadeMat;
            obstacle.GetComponent<Renderer>().materials[1] = fadeMat;

– nothing happens at all except I get an error that it’s out of the range. I’m really not sure what to do.

Update
I am aware that the material should be set to transparent or fade in order for it to change in opacity, however there must be some other viable solution as setting the materials shader to standard so I can change it’s type to fade causes the tree prefab to kick out an error that it needs to be an included shader or the custom one needs to include a specific line of code. Again, though, I know next to nothing about shaders.

Both of the code examples you shared I would say have similar issues. You are failing to copy the color back into the material, or you are failing to copy the materials array back into the renderer.

To change the color of the material you need to do something like:

Material mat = Hitinfo.collider.gameObject.GetComponent<Renderer>().material;
Color c = mat.color;
c.alpha = whatever;
mat.color = c;

To change the materials array you need to do something like this:

Renderer r = obstacle.GetComponent<Renderer>();
r.materials = new Material[] { fadeMat, fadeMat };

So I had tried something similar (I think), like this –

    public GameObject player;

    public void Update()
    {
        DetectObstacle();
    }

    public void DetectObstacle()
    {
        GameObject obstacle;
        Vector3 towardsPlayer = (player.transform.position - Camera.main.transform.position).normalized;
        RaycastHit hitInfo;
        Physics.Raycast(Camera.main.transform.position, towardsPlayer, out hitInfo);
        Debug.DrawLine(Camera.main.transform.position, player.transform.position);
        obstacle = hitInfo.collider.gameObject;
        if (obstacle.CompareTag("Tree"))
        {
            Debug.Log("Obstacle Detected! Obstacle: " + obstacle.name + " detected at: " + obstacle.transform.position);
            Color obstacleOriginalColor = obstacle.GetComponent<Renderer>().material.color;
            Color obstacleFadedColor = new Color(obstacle.GetComponent<Renderer>().material.color.r, obstacle.GetComponent<Renderer>().material.color.g, obstacle.GetComponent<Renderer>().material.color.b, .01f);

            obstacle.GetComponent<Renderer>().materials[0].color = obstacleFadedColor;
            obstacle.GetComponent<Renderer>().materials[1].color = obstacleFadedColor;


        }
    }

– which resulted in this –

7558066--934756--ezgif.com-gif-maker (11).gif

and you can’t quite make it out, but the debug shows the log where the ray hit the tree.

Update
There is nothing happening in the above GIF, it doesn’t show any visible change is what I meant.

Bump

So the problem seems to be that the built in tree shader does not support fade by alpha. I’m not sure how to get it to do so…