- Home /
Can't use script on more items
I got this script and want to use it on several items, but when I try, the text doesn't show up anymore.
Every item gets their own canvas, but is there a better way? (The names show up above the object)
It works when I put it on 1 object. As soon as a try 2 the text will be gone.
     public string myString;
     public Text myText;
     public float fadeTime;
     public bool displayInfo;
 
      private void Awake()
     {
         myText = GameObject.Find("Text").GetComponent<Text>();
         myText.color = Color.clear;
 
     }
 
     private void Update()
     {
         FadeText();
 
         
     }
 
 
     private void OnMouseOver()
     {
         displayInfo = true;
         
     }
 
 
     private void OnMouseExit()
     {
         displayInfo = false;
     
     }
 
     
     void FadeText()
     {
 
        if (displayInfo)
         {
           
             myText.text = myString;
 
             myText.color = Color.white;
         }
         else
         {
             
             myText.color = Color.clear;
         }
     }
 
 }
 
 
 
               Comment
              
 
               
              Answer by Indiana-Jonas · Jan 10, 2019 at 09:38 AM
It looks like you're always referencing the same object in your Awake() function. Try to make that a variable or simply write
 myText = GameObject.GetComponent<Text>();
To reference the text component on the gameObject that you've attached the script to. Otherwise you're constantly overwriting everything.
I get a lot of errors when I try to do what you say, how do I make that a correct variable?
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                