0
\$\begingroup\$

This script change the text color to white:

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

public class DisplayInfo : MonoBehaviour
{
    public string myString;
    public Text myText;
    public float fadeTime;
    public bool displayInfo = false;

    // Use this for initialization
    void Start()
    {
        if (myText == null)
        {
            Debug.LogError(myText + " Is null please add Text component !");
        }

        if (myString == "")
        {
            Debug.LogWarning(myString + " Is empty, No text will be display.");
        }

        myText.color = Color.clear;
    }

    // Update is called once per frame
    void Update()
    {
        FadeText();
    }

    void FadeText()

    {
        if (displayInfo)
        {
            myText.text = myString;
            myText.color = Color.Lerp(myText.color, Color.white, fadeTime * Time.deltaTime);
        }

        else
        {
            myText.color = Color.Lerp(myText.color, Color.clear, fadeTime * Time.deltaTime);
        }
    }
}

This is the script i'm using with the DisplayInfo:

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

public class DetectInteractable : MonoBehaviour
{
    public Camera cam;
    public float distanceToSee;
    public string objectHit;
    public Image image;
    public bool interactableObject = false;
    public DisplayInfo displayInfo;
    public Canvas canvas;

    private RaycastHit whatObjectHit;

    private void Update()
    {
        Debug.DrawRay(cam.transform.position, cam.transform.forward * distanceToSee, Color.magenta);

        if (Physics.Raycast(cam.transform.position, cam.transform.forward, out whatObjectHit, distanceToSee))
        {
            if (whatObjectHit.collider.gameObject.tag == "Interactable")
            {
                image.color = Color.red;
                objectHit = whatObjectHit.collider.gameObject.name;
                interactableObject = true;
                displayInfo = whatObjectHit.collider.gameObject.GetComponent<DisplayInfo>();
                displayInfo.displayInfo = true;
            }
            else
            {
                image.color = Color.white;

                if (displayInfo != null)
                    displayInfo.displayInfo = false;
            }
        }
        else
        {
            image.color = Color.white;

            if (displayInfo != null)
                displayInfo.displayInfo = false;
        }
    }
}

When it's "Interactable" then i display the info about this object.

The DetectInteractable is attached to the Player (FPSController) in the hierarchy:

Attach on player

And on each "Interactable" item i attach the script DisplayInfo: For example the door:

door

And this is the editor screenshot before running the game: Display Infomration is Canvas. The Text i set it's color to white in the inspector.

Canvas

But when running the game the text i see on objects is gray and i see that on the inspector the white is becoming gray: On the game view window there is a text Security Door in fray it's hard to see it. It should be in white and bright enough to see it clear:

Canvas1

\$\endgroup\$
  • \$\begingroup\$ Can you show us what exactly "mytext,mystring" etc.. are assigned to in the inspector? \$\endgroup\$ – user100681 Nov 12 '17 at 9:59
  • \$\begingroup\$ Your script doesn't make much sense. I think your mental model about what Lerp does is a bit warped. Unfortunately it's hard to tell what exactly you misunderstood. What exactly do displayInfo and fadeTime mean in your script? \$\endgroup\$ – Philipp Nov 12 '17 at 11:18
  • \$\begingroup\$ @Philipp DisplayInfo is meant to display some information about game objects in the game. It should show text describing the object. In the script DetectInteractable if the object is "Interactable" i want to show a text above the object in the game view while the game is running. So i added the DisplayInfo script to some game objects using same Text child of the Canvas and in the DetectInteractable i'm setting when to show or not the text. In the DisplayInfo i just want to display the text it's not have to be with Lerp or with Fade (Fading could be nice). \$\endgroup\$ – user1196715 Nov 12 '17 at 18:18
  • \$\begingroup\$ @Philipp When the object is "Interactable" display text. Other don't display anything. \$\endgroup\$ – user1196715 Nov 12 '17 at 18:21
1
\$\begingroup\$
public float t = 0f;

// Use this for initialization
void Start()
{
    if (myText == null)
    {
        Debug.LogError(myText + " Is null please add Text component !");
    }

    if (myString == "")
    {
        Debug.LogWarning(myString + " Is empty, No text will be display.");
    }

    myText.color = Color.clear;
}

// Update is called once per frame
void Update()
{
    FadeText();
}

void FadeText()

{
    if (displayInfo)
    {
        myText.text = myString;
        myText.color = Color.Lerp(Color.clear, Color.white, t);
        if (t < 1)
        {
            t += Time.deltaTime / fadeTime;
        }
    }

    else
    {
        myText.color = Color.Lerp(Color.white, Color.clear, t);
        if (t < 1)
        {
            t += Time.deltaTime / fadeTime;
        }
    }
}

this should work as you want
make t = 0 every time you want to show or fade the text
if the color still not changed then check if there is other script that change the text color

other solution use dotween
it's faster than figuring how Lerp is working

\$\endgroup\$
  • 1
    \$\begingroup\$ You might want to show how to reset t when changing displayInfo. For instance, putting this inside a setter so we can't accidentally change one without the other. \$\endgroup\$ – DMGregory Nov 12 '17 at 14:51
  • \$\begingroup\$ For some reason it's not working for the first object but it does for the second. I used a break point it's getting inside the if (displayInfo) and make the lines myText.text = myString; myText.color = Color.Lerp.......but it's not showing any text. But when it's making the if (displayInfo) on the second object it's working fine. \$\endgroup\$ – user1196715 Nov 12 '17 at 16:33
  • \$\begingroup\$ @DMGregory Where do i reset the t ? \$\endgroup\$ – user1196715 Nov 12 '17 at 18:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.