Hello. There is a task to make a button with a picture and so that when you hover over this button the picture changes.

Only one solution came to mind:

  • Created a button

  • Created a picture (Image) as a child object in Button

  • When you hover the button hung an event.

And the whole problem is to change the sprite of the child object. Trying to change the sprite as follows:

if (this.transform.Find ("Image")) { Debug.Log ("OnHover"); Image bImg = this.GetComponent<Image>(); bImg.sprite = ButtonImageHover; } 

But for some reason, the sprite does not change, although "OnHover" runs through the console when you hover over the button ...

  • one
    Context is lost. Try this: Image bImg = this.transform.Find ("Image"). GetComponent <Image> (); - Valera Kvip

4 answers 4

VeryBadUser, then I would do otherwise.

Find operation can reduce performance. And there may be problems if there are several objects on the scene called "Image". And then not the fact that it will be a child object.

GameObject.Find for example, inside MonoBehaviour.Awake or MonoBehaviour.Start. This is every function. MonoBehaviour.Start is a common pattern to assign a GameObject to a variable inside MonoBehaviour.Update.

  public class MyClass { public Image bImg;// Можно назначить через инспектор. void Awake() { bImg = transform.Find("/Image").GetComponent<Image>();// Или найти среди дочерних объектов. } void F() { if (bImg != null) { Debug.Log ("OnHover"); bImg.sprite = ButtonImageHover; } } } 

    Such things can be done in two ways without any code:

    1. Through the animation of the state of the buttons, you can configure the change of state of several objects according to your desire (the official tutorial is somewhere from 5:30 about the animation of the button ).
    2. Add an EventTrigger button and add event handling to it under the pointer ( PointerEnter ) and the pointer is gone ( PointerExit ) enter image description here

    The smaller the code, the more flexible will be the ability to customize your buttons)

    • Initially, I decided to do everything through the code, since There are about 15 buttons on the screen (with settings, game modes, forward, back, etc.) and I wanted to simplify their settings, but in the end you still have to throw at least 2 sprites on each button, but still faster if you set for each triggers))) (for advice, plus)) - VeryBadUser

    This is done without any code at all.

    Use the Button element and set everything you need in the inspector settings

    • yes, the option is not bad, but everything is already taken there (i.e. my button already uses several sprites (the button is in normal condition - 1 sprite, the hover button - 2 sprite). That is, in fact, I want replace the button text with the picture - VeryBadUser

    Thank you Valera Kvip. Used your solution:

     if (this.transform.Find ("Image")) { Debug.Log ("OnHover"); Image bImg = this.transform.Find ("Image").GetComponent<Image>(); bImg.sprite = ButtonImageHover; }