- Home /
Text show when hovering object in Unity 2019.4.12f1
I am making a simple build where i can pan, zoom and rotate an object in the middle. And it works fine....After some struggle.
Now i am stuck with compiler error again when trying to make text appear when my cursor hovers over a Gameobject.
Why do i get compiler error in this script? I have done everything the tutorial showed me?
I do not get any messages in the console either.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Varenummer : MonoBehaviour
{
public string myString;
public Text myText;
public float fadeTime;
public bool displayInfo;
void Start()
{
myText = GameObject.Find ("Text").GetComponent<myText> ();
myText.color = Color.clear;
}
void Update()
{
FadeText();
}
void OnMouseOver()
{
displayInfo = true;
}
void OnMouseExit ()
{
displayInfo = false;
}
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);
}
}
}
I guess you get it at line 16, ....GetComponent(). If you don't have a class "myText" inherited from $$anonymous$$onoBehaviour, it cannot get it through the getComponent call. If you want to get the text component from your gameobject "Text", you should use the class name in the component call, i.e.
myText = GameObject.Find("Text").GetComponent(); Please add the compiler message, or at least the line where it fires, next time.
You are very right Klarzahs, line 16 was the error.
I am pretty new and now have the console ready for next time.
Thank you :)
Answer by rhapen · Oct 26, 2020 at 03:52 PM
My guess is same as Klarzahs in the comment
myText = GameObject.Find("Text").GetComponent<myText>();
Is your game object with text called maybe myText? If yes then i guess you have it rotated
myText = GameObject.Find("myText").GetComponent<Text>();
That was excactly it.....I had it rotated.
Good call rhapen. I appreciate it <3
Thank you.