- Home /
How to add a Sprite Rollover Image using a Button Array
Hello!
I am trying to add a "Normal" and "Selected" Sprite Rollover Image to my buttons using this script. I tried adding a bool buttonIsSelected to check whether or not the button in the Array has been clicked on and is now "Selected" but its currently not working.
The debug log is still saying ""Button Normal ROLLOVER" even after I have clicked on the button and the sprite has changed to "Selected" sprite.
Here is my script:
public class GameManager : MonoBehaviour
{
public Button[] buttons;
public Sprite normal;
public Sprite selected;
public Sprite normalRollover;
public Sprite selectedRollover;
public bool buttonIsSelected;
void Start()
{
buttonIsSelected = false;
}
// Button Pressed
public void ButtonPressed(int buttonNum)
{
for (int i = 0; i < buttons.Length; i++)
{
if (i == buttonNum)
{
buttons[i].GetComponent<Image>().sprite = selected;
buttonIsSelected = true;
}
else
{
buttons[i].GetComponent<Image>().sprite = normal;
}
}
}
// Button Rollover
public void ButtonPointerEnter(int buttonNum)
{
for (int i = 0; i < buttons.Length; i++)
{
if (i == buttonNum && buttonIsSelected == true)
{
Debug.Log("Button Selected ROLLOVER");
// buttons[i].GetComponent<Image>().sprite = selectedRollover;
}
else
{
Debug.Log("Button Normal ROLLOVER");
// buttons[i].GetComponent<Image>().sprite = normalRollover;
}
}
}
}
Any help/advice you could offer on how to go about fixing my script would be greatly appreciated!
Thank you so much!
Comment