Question by
JannickL · Oct 31, 2016 at 10:44 AM ·
scripting problemuibuttons
How to shorten this code?
Hey guys, i dont want to repeat the void like i did there. So how can i shorten the code and write a function that inserts what i want?
This script is not attached to the buttons. The buttons are calling their connected (inspector) void.
private bool isSelected;
private int passiveSelected;
public Image hook1; // A green hook to show which one is selected
public Image hook2;
public Image hook3;
public void SetPassive1() // If button nr. 1 is clicked
{
passiveSelected = 1;
hook1.enabled = true;
hook2.enabled = false;
hook3.enabled = false;
}
public void SetPassive2() // If button nr. 2 is clicked
{
passiveSelected = 2;
hook1.enabled = false;
hook2.enabled = true;
hook3.enabled = false;
}
public void SetPassive3() // If button nr. 3 is clicked
{
passiveSelected = 3;
hook1.enabled = false;
hook2.enabled = false;
hook3.enabled = true;
}
Comment
Answer by doublemax · Oct 31, 2016 at 12:56 PM
It's easier if you put the Images into an array:
public Image[] hooks;
public void SetPassive( int index )
{
passiveSelected = index;
for( int i=0; i<hooks.Length; i++ )
hooks[i].enabled = (i == index);
}