Hi there!
I have a list of sprite gameObjects that I would like to be displayed in the GUI as a sort of inventory, but I’m unable to access the texture. I would like the buttons to have the same image on them as the sprite.
I’m probably going about this in the wrong way, but I thought this would be the simplest way to display an item that had been picked up into the inventory.
Any assistance would be greatly appreciated!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BrainScript : TouchScript {
public List<GameObject> Inventory;
}
void Start () {
Inventory = new List<GameObject>();
// Adding a gameobject manually for testing
Inventory.Add(GameObject.Find("goldCoin"));
Debug.Log("Inventory contains: " + Inventory[0]);
Debug.Log(Inventory[0].renderer.material.mainTexture);
Debug.Log(Inventory[0].renderer.material.GetTexture("_MainTex"));
}
void OnGUI()
{
GUILayout.BeginHorizontal ();
foreach(GameObject obj in Inventory)
{
//Texture2D testTexture = obj.renderer.material.mainTexture as Texture2D;
Texture2D testTexture = obj.renderer.material.GetTexture("_MainTex") as Texture2D;
GUILayout.Button(testTexture);
}
GUILayout.EndHorizontal();
}
Debug.Log’s output:
Inventory contains: goldCoin (UnityEngine.GameObject)
Null
UnityEngine.Debug:Log(Object)
Null
UnityEngine.Debug:Log(Object)
The buttons are displayed, but with no contents.