New to the new UI (buttons)

I need help! I’m not used to the new Unity UI system, as far as I know you can only add individual scripts to buttons. However, in my situation I want one script to handle the functionality of many buttons attached to an empty game object or something. Heres an image of what I want to try and program currently:

55737-unity-trouble.png

For the current arrow buttons I want the word to change from skin → hair - > eyes as pressed accordingly, how would I go about this with the new system? Make a global integer variable and use that in each button script??

If you want to control your button elements from a centralized location you first need to get them with GameObject.Find or GameObject.FindGameObjectWithTag or something similar to get a reference to the GameObject containing the button. Then you get the button component from that object with GetComponent<Button>(). Even though you don’t have access to the editor’s onClick event menu you can still assign functions as the button’s listener by using generic functions (lambda expressions). Basically it looks something like this:

myButton.onClick.AddListener(() => MyFunction());

This will attach the handler that makes MyFunction run whenever the button is clicked. But the advantage of generic functions is that you can make the lambda expression contain the contents of the function itself. A lot of the time a button is only meant for a simple task, like switching a bool or setting a value, so it pays to have a way to add that functionality without a lot of extra mess in your code. To do it you’d just do:

myButton.onClick.AddListener(() => { Debug.Log("Foo"); });

And then you have a way to add as much functionality as needed to a button from any external class without having to worry about needing a script for each and every button.

Try:

public void OnGUI(){
		if(GUI.Button(new Rect(10,70,20,30), "label")){
			//Your functionality code goes here
		}
	}

Where 10 and 70 are the location (X and Y) and 20 and 30 are the size (X and Y).
Sorry if my text is a bit confusing.

Link to documentation: Unity - Scripting API: GUI.Button