The question is answered, right answer was accepted
Change UI button state to "pressed" via script
Good evening,
I have some UI buttons, but as my game is for Android, I'm having some problems with changing the state of the button, I have one sprite for the button and one for it when pressed. The problem is, I can't change the state of the button. Bellow is the script I'm using the check if the button was touched, and I'm sending a message to it. How do I change the state of this button so it can show the right sprite (Pressed)?
void Update () {
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) {
RaycastHit2D buttonHit;
buttonHit = Physics2D.Raycast(this.transform.position, Input.touches[0].position);
if (buttonHit)
{
objectHit = buttonHit.transform.gameObject;
if (objectHit.name == "Button_Play")
{
objectHit.SendMessage("LoadScene",1,SendMessageOptions.DontRequireReceiver);
}
}
}
}
Answer by hexagonius · Dec 19, 2015 at 06:53 AM
are you using Unity UI buttons? if not, why not? if yes, check this out, no code needed. Just create a UI button change transition and assign sprite, done
Good $$anonymous$$orning @hexagonius,
Thanks for the reply. And yes, I'm using UI button, the problem is, when I touch the button, the script recognize the touch, but don't know which object was touched. That is why I use the Raycast to find out which object was touched. As you see in the script, I'm already sending a message to the button. I tried to send a message "OnClick" but didn't work. If there is a better way to recognize a button touch on a Touch Screen, please let me know.
Follow this Question
Related Questions
activate button on triggerenter 0 Answers
I want to press a Button and play a sound? 2 Answers
Spawn prefab on UI button click 0 Answers
Unity Button Script 0 Answers