- Home /
How do I access a GUITexture from C#?
I am trying to hide/display a GUITexture when another GUITexture is pressed. How would I do this?
I am trying to access it this way,
gameObject.GetComponent("GUITopButton").guiTexture.enabled = true;
But I get nullvalue. How would I access GUITopButton?
First, GetComponent
receives a component type as parameter, and it retrieves a specific component from a given gameobject. It does not find a GameObject. I personally always prefer using the generic version of GetComponent
in C#:
gameObject.GetComponent<GUITexture>().enabled = true;
Second, if you use the .guiTexture
to access it directly (which is better than GetComponent
), you don't need to use GetComponent at all:
gameObject.guiTexture.enabled = true;
Last, if you need to find the GameObject itself by name (if the script is not attached to the gameobject that has the GUITexture component, but to another GameObject), then you can use GameObject.Find()
:
GameObject.Find("GUITopButton").guiTexture.enabled = true;
Your answer
Follow this Question
Related Questions
Button don't work? 1 Answer
Multiple Cars not working 1 Answer
Unity 2D - Basic Button Movement 1 Answer
Need help with GUI script. (C#) 2 Answers
Touchless GUI Interface - Button Mouseover Emulation 1 Answer