casting gameObject as a GUITexture

How can I cast from Game Object to GUI Texture in C#.. I have tried the following ways but they don't work.

public GUITexture BtnArrow;

void Start() {

//1st way

BtnArrow = (GUITexture)GameObject.Find("GUI_LeftArrow");

//2nd way

BtnArrows= GameObject.Find("GUI_LeftArrow") as GUITexture;

}

GUITexture is actually a component, so you need to do (GUITexture)myGameObject.GetComponent(typeof(GUITexture)) (in C#). I can't remember, but there might be myGameObeject.guiTexture too.

You can first find the GameObject, then find the component you want:

GameObject go = GameObject.Find("GUI_LeftArrow");
BtnArrow = (GUITexture)go.GetComponent(typeof(GUITexture)); // note: could use generics here

However, for common components like Transform, GuiText, Collider, etc., Unity has shortcut variables. See GameObject docs for the complete list.

BtnArrow = GameObject.Find("GUI_LeftArrow").guiTexture;

Probably better to do this in two steps so that your code won't crash if the object isn't found:

GameObject go = GameObject.Find("GUI_LeftArrow");
if ( go != null )
    BtnArrow = go.guiTexture;

Other alternatives: you can walk all objects in the scene of a type by calling FindObjectsOfType, or if there is only one of that type, you could call FindObjectOfType.

Note: above code is not tested, but it should be pretty close.