Scripted GUI Button + Hover Problem

I’m generating a small GUI COntainer on the upper side of the Screen, where I want to display all buildable Structure for my Prototype.
The Building Prefabs are containing an Icon for each normal and hover view (*-Ro (Rollover) Suffix).
The Prefabs are Located in resources/Buildings.

Code for generation (MenuSetup.cs):

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MenuSetup : MonoBehaviour {
    public static List<Texture2D> UnitIconTextures = new List<Texture2D>();
    public static List<Texture2D> UnitIconTexturesRo = new List<Texture2D>();
    public static List<string> UnitNames = new List<string>();
    public static List<string> UnitPaths = new List<string>();
    public Texture2D IconContainer = null;

    void OnGUI () {
        GUIStyle Container = new GUIStyle();
        Container.normal.background = IconContainer;
        //GUI Container
        GUI.Box(new Rect(Screen.width / 2 - 200, Screen.height - 40, 400,500),"", Container);
        //Iconsbuttons

        int offset = 48;
        int j = 0;
        for(int i = 0; i < UnitNames.Count; i++)
        {
            Debug.Log(UnitNames[i]);
            GUIStyle Icon = new GUIStyle();
            Icon.normal.background = UnitIconTextures[i];
            Icon.hover.background = UnitIconTexturesRo[i];

            if (GUI.Button(new Rect(Screen.width / 2 - 195 + (offset * j), Screen.height - 39, 46, 39), "", Icon)) ;
            {
                Debug.Log(UnitNames); // Show building name on click
            }
            j++;
        }
    }
}

WorldUI.cs

using UnityEngine;
using System.Collections;

public class WorldUI : MonoBehaviour {
    void Start () {    
        string path = "Buildings/";
        Object[] Buildings = Resources.LoadAll(path);
        if(Buildings.Length > 0) {
            for(int i =0; i < Buildings.Length; i++)
            {
                GameObject build = Buildings[i] as GameObject;
                Texture2D Icon = build.GetComponent<Building>().MenuIcon;
                Texture2D IconRo = build.GetComponent<Building>().MenuIconRo;
                Debug.Log(Buildings[i]);
                MenuSetup.UnitIconTextures.Add(Icon);
                MenuSetup.UnitIconTexturesRo.Add(Icon);
                MenuSetup.UnitNames.Add(build.name);
                MenuSetup.UnitPaths.Add(path+"/"+build.name);
            }
        }
    }
}

And the relevant part of the Building-Class:

    public Texture2D MenuIcon;
    public Texture2D MenuIconRo;

The Icons are rendered in the Container and it looks like this:
But if I honver over the Icon, the Background doesnt turn into this and also dont display anything in the console on Click.

I hope you can help me this this.

any particular reason you’re holding onto the legacy OnGUI approach to in game ui? it was replaced by the canvas approach a while back which is a lot simpler to put together and it would do the hover thing by default…

1 Like

Maybe because my books and tutorials are outdated^^
Is there an option to adapt the code to the new version without changing too much?

I just made a canvas with a child for the container, can you link me any helpful reference how I can script the buttons and handle the actions? I assume using a prefab button an load the image via parameter