I have a simple UI Set up, that uses a few GUI Texture on Screen, to act as the player controls. I want to simply say, “When this GUITexture is Pressed, Do this”. Here’s my problem. To make it way easier on me, I’m using Public variables in a “Master Script”. So I can simply drag an drop all of my textures into the inspector, and everything is set up for me. How can I call OnMouseDown (Or something similar) on these GUITexture variables? Thanks!
This is my example of how I would do it
Script per GUITexture:
public class GUITextureClick : MonoBehaviour
{
MasterScript masterScript;
void Start()
{
//Obviously has to be changed to fit your setup
masterScript = GameObject.Find("/MyMasterScriptObject").GetComponent<MasterScript>();
}
void OnMouseDown()
{
MasterScript.OnMouseDown(this.gameObject);
}
}
Your master script would implement this function:
public void OnMouseDown(GameObject sender)
{
switch(sender.transform.name)//One way to check which texture got clicked
{
case "Button1":
break;
}
}
All you would have to do now is assign the GUITextureClick component to all textures, if you think THAT would be a hassle then that’s also quite simple, just select all of them and assign the component through the component menu: Component->Scripts->GUITextureClick once it’s added to your project.