- Home /
GUI error with texture.
My GUI button is having an error I have an code assigned texture and so on, but the only thing the button does is say Firewood(Unity Engine Texture) in stead of showing a texture2d,(Firewood is the texture file name). I don't know why, please help(My texture is 40x40, and so is the button). Cheers.
Heres the script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerInventory : MonoBehaviour {
public List<Item> items = new List<Item>();
public string PrimaryS;
public string SecondaryS;
public Gun Primary;
public Gun Secondary;
[HideInInspector]
public Texture2D gun1icon;
[HideInInspector]
public Texture2D gun2icon;
public GUIStyle gunStyle;
public GUIStyle itemStyle;
private int rows = 4;
private int cols = 4;
private Rect windowRect = new Rect(150,150,200,300);
private bool isWindow;
public static PlayerInventory ins;
// Use this for initialization
void Start () {
ins = this;
}
// Update is called once per frame
void Update () {
// gun1icon = Resources.Load("Icons/" + PrimaryS);
// gun2icon = Resources.Load("Icons/" + PrimaryS);
if(Input.GetKeyUp(KeyCode.G)){
isWindow = !isWindow;
}
}
void LateUpdate(){
foreach(Item im in items){
im.texture = Resources.Load("Icons/" + im._name) as Texture2D;
im.BuiltPrefab = Resources.Load("Prefabs/" + im._name) as GameObject;
}
}
void OnGUI(){
//Start Item List
if(isWindow)
windowRect = GUI.Window(0,windowRect,WindowPlayer,"Gear");
}
public void AddAnItem(string nameofItem){
Item temp = new Item();
temp._name = nameofItem;
temp.texture = Resources.Load("Icons/" + nameofItem) as Texture2D;
temp.BuiltPrefab = Resources.Load("Prefabs/" + nameofItem) as GameObject;
items.Add(temp);
}
public void RemoveItemFromList(int _PositionInList){
items.RemoveAt(_PositionInList);
}
public void WindowPlayer(int windowID){
if(GUI.Button(new Rect(25,20,150,50),"")){}
if(GUI.Button(new Rect(25,75,150,50),"")){}
int cnt = 0;
for(int y = 0; y < rows; y++){
for(int x = 0; x < cols; x++){
if(cnt < items.Count){
if(GUI.Button(new Rect(20 + x * 40, 130 + y * 40,40,40),"" + items[cnt].texture)){}
}
else{
GUI.Box(new Rect(20 + x * 40, 130 + y * 40,40,40),"");
}
cnt++;
}
}
GUI.DragWindow();
}
}
[System.Serializable]
public class Item{
public string _name;
public Texture2D texture;
public GameObject BuiltPrefab;
public bool isCounted;
public bool isCountedAsNull;
}
Answer by Joyrider · Aug 10, 2013 at 11:34 AM
Hehe, that sounds like it is converted to a string, which I sometimes use in debugs.
I'm guessing the problem is at line 81:
if(GUI.Button(new Rect(20 + x * 40, 130 + y * 40,40,40),"" + items[cnt].texture)){}
remove the "" + , it indicates the type is a string, and thus your texture is interpreted as one, giving you: ObjectName(ObjectType).
So it should be :
if(GUI.Button(new Rect(20 + x * 40, 130 + y * 40,40,40),items[cnt].texture)){}
Your answer
![](https://koobas.hobune.stream/wayback/20220613111914im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Populate inventory with item icons 1 Answer
Inventory, why didn't work element? 1 Answer
My OnGUI() Won't show the Button elements :( 0 Answers
Draw crosshair INSTEAD of cursor. 1 Answer