Show the texture when hovering a button

Hi guys, I want to show the texture when hovering a button. But when I tried below code, the texture is not appears, even though the mouse already hover the button. When I tested it on the OnGUI function, it is appears correctly, just not show when hovering on a button.

How could I fix this?

Here is the code that I am using:

using UnityEngine;
using System.Collections;

public class MapSelection : MonoBehaviour 
{
    private Rect villageRect, yeinPlainRect; // Define the Rect

    public GUISkin customMapButton = null; // Define for the custom map buttons

    public Texture villageInformation = null; // For the information

    public static bool isEnteredVillage = false; // For checking if the player entering the village from the map

    private void Start()
    {
        // Set the Rect for the village
        villageRect = GameManager.CenterOnScreen(75, 20, 500, 250);

        // Set the Rect for the yein plain
        yeinPlainRect = GameManager.CenterOnScreen(75, 20, 550, 125);
    }

    private void Update()
    {
        // This below code is not run

        // If the mouse hovering village button
        if (villageRect.Contains(Input.mousePosition))
        {
            // Draw the texture
            GUI.DrawTexture(new Rect(villageRect.x, villageRect.y, 500, 325), villageInformation, ScaleMode.ScaleToFit, true);
        }

        // End of it
    }

    private void OnGUI()
    {
        // Call the SetButtons function
        SetButtons();

        // I have tested it, and the image show on the screen like the second image below:
        // Draw the texture
        GUI.DrawTexture(new Rect(villageRect.x - 100, villageRect.y - 35, 500, 325), villageInformation, ScaleMode.ScaleToFit, true);

        // End of it
    }

    private void SetButtons()
    {
        // If the button been clicked
        if (GUI.Button(villageRect, "Village", customMapButton.customStyles[0]))
        {
            // Load another level
            GameManager.LoadLevel("Third Loading Scene");
        }
    }
}

GameManager class:

public static Rect CenterOnScreen(int width, int height, int minusWidth, int minusHeight)
    {
        Rect _center = new Rect(Screen.width - (Screen.width / 2) - minusWidth, Screen.height - (Screen.height / 2) - minusHeight, width, height);

        return _center;
    }

Here is the screenshot:

First Image (The button on hovering):

36236-capture.png

Second Image (The button is not on hovering):

[36237-capture+2.png|36237]

Thank you guys

Your answer much appreciated!

add a little switch(boolean) for whether the rect for the village button is hovered and switch it in the update method. GUI calls should be done from OnGUI, that can be directly or in a method, doing it in Update is a failure to read the documentation.

Add a private field to your code(in the class definition, not in a method).

private bool isHoveringVillageButton = false;

// Update your other Update method.

private void Update()
{
  // This below code is not run
 
  // If the mouse hovering village button
  if (villageRect.Contains(Input.mousePosition))
  {
    isHoveringVillageButton = true;
    // Draw the texture
    // Not sure which DrawTexture you need to use, you can figure that out.
    // GUI.DrawTexture(new Rect(villageRect.x, villageRect.y, 500, 325), villageInformation, ScaleMode.ScaleToFit, true);
  }
  else
  {
    isHoveringVillageButton = false;
  }
 
// End of it
}

// Check your new isHoveringVillageButton field and display the texture if true

private void OnGUI()
{
  // Call the SetButtons function
  SetButtons();
 
  // I have tested it, and the image show on the screen like the second image below:
  // Draw the texture
  if (isHoveringVillageButton)
  {
    GUI.DrawTexture(new Rect(villageRect.x - 100, villageRect.y - 35, 500, 325), villageInformation, ScaleMode.ScaleToFit, true);
  }

  // End of it
}