Problem fading in child text objects

Please excuse the newbie question…

I’m trying to fade in text objects under a parent object upon collision by changing the alpha value of each one, but the texts appear right away with the full alpha value assigned to them in the inspector.

Thanks for whatever insight you can offer!

Here is my c# script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DisplayOnCollision : MonoBehaviour
{
    private Renderer r;
    private Component[] renderers;
    public float duration = 3.0f;
    bool hit = false;

    void Start ()
    {
        r = GetComponent<Renderer> ();
        renderers = gameObject.GetComponentsInChildren<Renderer> ();
        r.enabled = false;
        foreach (Renderer rc in renderers)
            rc.enabled = false;
    }

    void Update ()
    {
        if (hit) {
            r.enabled = true;
            foreach (Renderer rc in renderers) {
                rc.enabled = true;
                Color curColor = rc.material.color;
                float ratio = Time.time / duration;
                curColor.a = Mathf.Lerp (0, 1, ratio);
                rc.material.color = curColor;
            }

        } else {
            r.enabled = false;
            foreach (Renderer rc in renderers) {
                Color curColor = rc.material.color;
                curColor.a = 10.0f;
                rc.material.color = curColor;
                rc.enabled = false;
            }
        }
    }

    void OnTriggerEnter (Collider other)
    {
        if (other.CompareTag ("Cam")) {
            hit = true;
            Debug.Log ("Hit!");
        }
    }

    void OnTriggerExit (Collider other)
    {
        hit = false;
    }
}

Well, I would suggest a tween engine for doing this sort of thing, but skipping that. if you need to fade a parent and all children, a canvas group on the parent would be better as it has an alpha that you can use to fade out the children without having to loop.

https://docs.unity3d.com/Manual/class-CanvasGroup.html

The second thing is color takes values of 0-1 for each value. Which means if you want a alpha that is 255, you set it’s value to 1. So by setting curColor.a = 10f, you’re actually just telling it to use max alpha. Not 10/255 alpha. If you want 10f, you would need to divide 10 by 255 to get the value you want. (so curColor.a = 10f/255f or curColor.a = .039f) Note, this is also why when the game starts, you get full alpha right away.

Now, the other thing is your update right now checks for hit. But when hit is false, it constantly loops through and hits the else statement. This is unnecessary. Either using coroutines that you can cancel or as I suggested earlier, a tweening engine where you can cancel the tween would be better.

Time.time increases over time. So your ratio:

float ratio = Time.time / duration;

Will pretty much always be 1. You’re dividing the time since the game started with the duration. There’s nothing that relates your fading to when the trigger enter happened.

You’ll need to store the time the hit happened, and check the difference between that and the current time:

//in OnTriggerEnter:
hitTime = Time.time;

//in Update:
float timeSinceHit = Time.time - hitTime;
float ratio = timeSinceHit / duration

Note that if there’s more than one thing that can hit the thing, this won’t work very well, as the OnTriggerEnter time will be reset every time something hits the trigger. You’ll have to decide how you want to handle that.

1 Like

Thanks folks. I normalized the alpha calculation and fixed the Time.time problem. It works!
I haven’t looked into the animation functionality yet, and have only used canvases for GUI text and such. Got a lot more to learn…

You may also want to use a CanvasGroup instead of changing each text’s alpha value individually.

As I also suggested. :stuck_out_tongue: But might have been lost in my bit of text.

Lol. OK, so since you suggested it first!..:wink:
In the spirit of exploration, I’ve added a canvas group component to my parent object, and modified the code, but now all the object hierarchy shows from the start of the game, with no changes to the alpha. Collisions are detected, but they don’t affect the Canvas Group’s alpha.
Code below:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DisplayOnCollision : MonoBehaviour
{
    public float duration = 3.0f;
    private float hitTime;
    private bool hit;

    private Renderer r;
    private CanvasGroup cg;

    void Start ()
    {
        r = GetComponent<Renderer> ();
        if (r == null)
            Debug.Log ("Cannot get renderer");
        cg = GetComponent<CanvasGroup> ();
        if (cg == null)
            Debug.Log ("Cannot get Canvas Group");
        r.enabled = false;
        cg.alpha = 0;

    }

    void OnTriggerEnter (Collider other)
    {
        if (other.CompareTag ("Cam")) {
            hitTime = Time.time;
            hit = true;
        }
    }

    void OnTriggerExit (Collider other)
    {
        hit = false;
    }


    void FixedUpdate ()
    {

        if (hit) {
            float timeSinceHit = Time.time - hitTime;
            float ratio = timeSinceHit / duration;
            cg.alpha = Mathf.Lerp (0, 1, ratio);
            r.enabled = true;
            Debug.Log ("Hit!");
        } else
            cg.alpha = 0;
        r.enabled = false;
        //Debug.Log ("Nothing!");
    }
}

Are these ui text objects? You mentioned text, so I think we assumed you were using unity text objects. To add, the suggestion was for UI stuff. I’m not sure a canvas group works for other things honestly.

Yeah, that was what I was wondering. No, these are game objects. NOT ui text.
Gonna try my hand at animating now…

Everything is a gameobject. :stuck_out_tongue: You don’t fade out gameobjects. You fade out a component. Which could be an image, sprite, text, etc. Good luck on animating!

TY Brathnann