I've got a problem and I am struggling with this for a couple of days now.
I am making an Editor script and I want to access buttons individually when created by a for loop. There is a public class ButtonNode which stores some variables of the button like name and size.
public class ButtonNode
{
public bool button;
public string name = "O";
public float sizeW = 25f;
public void AlterButton()
{
name = "P";
}
}
Then there is my Editor script in which I create the buttons. This is where I get stuck, how do I access these buttons individually? For example, I want to change the name of the button when that button is clicked. How do I do that? Because they are booleans (I don't really know why, this is the legacy GUI) I can't access the transform.name components etc).
public static ButtonNode[] bn = new ButtonNode[10];
void OnGUI()
{
GUILayout.BeginHorizontal();
for (int i = 0; i < bn.length; i++)
{
bn[i] = new ButtonNode();
if (bn[i].button = GUILayout.Button(bn[i].name + i, GUILayout.Width(bn[i].sizeW)))
{
bn[i].AlterButton();
}
}
GUILayout.EndHorizontal();
}
ButtonNode
objects each time, the OnGUI function is being called. You will certainly want to create them insideOnEnable
or at another point, to avoid overwriting the current buttons. \$\endgroup\$name
of each button from 'O' to 'P', then this should work. \$\endgroup\$bn = null
. You then check in OnGUI ifbn == null
and create the array and all buttons. The code should then look similar to this \$\endgroup\$