Search code examples
unity-game-engineunityscriptgameobject

Unity GetComponent(GUIText) bug?


I have an issue with the GetComponent(GUIText) the error i get is

There is no 'GUIText' attached to the "#######COUNTER(Clone)" game object, but a script is trying to access it.

Here is my code:

var UItecxt = GameObject.Find("#######COUNTER(Clone)");
var txtconvert = UItecxt.GetComponent(GUIText);
print(txtconvert);
txtconvert.text = counternumb.ToString();

I HAVE a GUIText on my clone! What is the issue? Thanks!


Solution

  • Your problem is that there is no GameObject named "#######COUNTER(Clone)" cloned in the scene. Run my code below and you will notice.

    var UItecxt = GameObject.Find("#######COUNTER(Clone)");
    var txtconvert : GUIText;
    
    if(UItecxt != null)
       txtconvert = UItecxt.GetComponent(GUIText);
    
    else
      Debug.Log("There was no GameObject with the name '#######COUNTER(Clone)' in the scene");
    

    To fix it just make sure you do have a GameObject with that name.