- Home /
Put sprite next to button on mouse over,
Heyo,
like the title states, I want to have an animated sprite that changes its position whenever the player mouses over one of the three buttons in my menu scene.
I've read some posts but I'm still quite unsure on how to make it work. I only found options to change the button's sprite itself.
Answer by Necromunger · Jan 08, 2017 at 05:41 AM
Firstly make a new script that implements the interface of IPointerEnterHandler as seen here: http://answers.unity3d.com/answers/1095070/view.html which will be a script for your buttons being hovered over.
Then set the position of your sprite gameobject you have a reference to, to an offset of that object.
For example:
public void OnPointerEnter(PointerEventData eventData)
{
spriteGameObject.GetComponent<RectTransform>().position = new Vector3(transform.position.x + OffsetX, transform.position.y + OffsetY);
}
Thanks a lot :) I've solved it rather ungracefully during the time the question was reviwed. I added an empty GameObject witha box collider as a trigger over every button and made it react to the Cursor with this script
public class ButtonGUI : $$anonymous$$onoBehaviour {
public GameObject p;
public float posX;
public float posY;
void On$$anonymous$$ouseEnter() {
p.transform.position = new Vector3(posX, posY, 0);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612104156im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Determining GUI button pressed 2 Answers
Gui Box - Texture - Spritesheet 0 Answers
GUI Texture Changes Position Depending On Screen Resolution? 1 Answer
GUI.enabled ? 0 Answers
How will i develop the type of gui? 0 Answers