Find GUIText object by tag


I have two GUIText objects with two different tags. I want to use get the handle to the GUIText object I want using its tag, like you can do with GameObject.FindWithTag.

For example I want to do this:

GUIText ThisText = (GUIText) GameObject.FindWithTag("asdf");

But it dosn’t work.
How do you accomplish this?

EDIT: error says:

error CS0029: Cannot implicitly convert type `UnityEngine.GameObject' to `UnityEngine.GUIText'

You’re trying to cast an Object into a component, which is not possible. Try that (C#):

GUIText ThisText = GameObject.FindWithTag("asdf").GetComponent<GUIText>() as GUIText;

Be careful, it’s a bold move. if the gameobject with asdf tag isn’t found, you’ll have a null exception. You might need to do that with a test instead.

There’s no such thing as a GUIText object, there are only GameObjects that have a GUIText component. Find the GameObject and then refer to the GUIText component.