# Why the canvas text is gray and not really white? [closed]

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 bool displayInfo = false;

// Use this for initialization
void Start()
{
if (myText == null)
{
}

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

myText.color = Color.clear;
}

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

{
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;
}
}
}


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

And on each "Interactable" item i attach the script DisplayInfo: For example the 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.

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:

• Can you show us what exactly "mytext,mystring" etc.. are assigned to in the inspector? – user100681 Nov 12 '17 at 9:59
• 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? – Philipp Nov 12 '17 at 11:18
• @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). – user1196715 Nov 12 '17 at 18:18
• @Philipp When the object is "Interactable" display text. Other don't display anything. – user1196715 Nov 12 '17 at 18:21

public float t = 0f;

// Use this for initialization
void Start()
{
if (myText == null)
{
}

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

myText.color = Color.clear;
}

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

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

else
{
myText.color = Color.Lerp(Color.white, Color.clear, t);
if (t < 1)
{
}
}
}


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

• 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. – DMGregory Nov 12 '17 at 14:51
• 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. – user1196715 Nov 12 '17 at 16:33
• @DMGregory Where do i reset the t ? – user1196715 Nov 12 '17 at 18:07