- Home /
Prevent source image change on click
I am creating a level as shown in below image where , when a button is clicked from group of buttons on the left and then any one of the 4 buttons on the right is clicked ,then the source image of the first clicked button will change to the image of the second clicked button . It works fine , but the problem is that when i try to do this on another button from the group previously selected button's source image will also change and all buttons will have same image . Is there a way where i can prevent change of source image of the button after click.
Code :
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class ControlBtn : MonoBehaviour { public Button Question;
public Button Answer1;
public Button Answer2;
public Button Answer3;
public Button Answer4;
public Sprite AnsImg1;
public Sprite AnsImg2;
public Sprite AnsImg3;
public Sprite AnsImg4;
public void ButtonClick()
{
Answer1.onClick.AddListener(ans1);
Answer2.onClick.AddListener(ans2);
Answer3.onClick.AddListener(ans3);
Answer4.onClick.AddListener(ans4);
}
void ans1()
{
Question.GetComponent<Image>().sprite = AnsImg1;
}
void ans2()
{
Question.GetComponent<Image>().sprite = AnsImg2;
}
void ans3()
{
Question.GetComponent<Image>().sprite = AnsImg3;
}
void ans4()
{
Question.GetComponent<Image>().sprite = AnsImg4;
}
}
Your answer
Follow this Question
Related Questions
How do I highlight multiple buttons at once? 1 Answer
how to insert hover effect on unity button and active color button when the button is being click ? 0 Answers
UI button mobile - want a button drag to register as a click 0 Answers
How to make the button respond to touch and hold feature? 5 Answers
Android Button : Select (highlight) by touch, before clicking. 2 Answers