Search Unity

Why doesn't this work OnGUI.button(new Rect(cordx,cordy,sizex,sizey),Inv[i].icon))

Discussion in 'Immediate Mode GUI (IMGUI)' started by Sneakki, Feb 24, 2015.

  1. Sneakki

    Sneakki

    Joined:
    May 29, 2013
    Posts:
    8
    I have this
    public class InvUI : MonoBehaviour
    { public Texture2D inv_small;// i have set an image in the inspector
    int btpx,btpy;
    int btsx,btsy;
    public class Item{
    int value;
    public Texture2D icon;
    Item()
    {value=0;

    }
    public int get_value(){ return value; }
    public Texture2D get_icon(){ return icon; }

    public void edit_value(int temp){
    value = temp;
    }
    public void edit_icon(Texture2D temp){
    icon = temp;
    }
    }
    Item[] Inv =new Item[100];
    void Start(){
    for(int x=0;x<18;x++)
    Inv[x].edit_icon(inv_small);
    }
    //as a nested class inside my main one; and this:
    void OnGUI(){

    GUI.Button (new Rect (btpx, btpy, 50, 50), Inv[1].icon);
    GUI.Button (new Rect (btpx + btsx, btpy, 50, 50), inv_small);
    GUI.Button (new Rect (btpx + btsx + btsx, btpy, 50, 50), inv_small);
    }
    void Update(){
    //here i set values for btp i bts variables
    }
    }



    and this doesnt work - doenst display buttons but it runs ... however if i change [Inv[1].icon / Inv[1].get_icon()] to just [inv_small] in the script it works just fine
     
    Last edited: Feb 24, 2015
  2. nygren

    nygren

    Joined:
    Feb 13, 2014
    Posts:
    33
    Aren't you getting a bunch of NullReferenceExceptions? You never create any Item objects. You have only created an array that can store 100 Item, but not added any Item to it. So Inv[1] is null and thus you can't get an icon from it.