Hmm… I don’t know enough about arrays to know what [toolbarInt] does. I’ve looked all over the internet to find good tutorials about how to make an inventory in unity. My method is clunky and requires allot of coding each time i add a new itemtype. Do you have any suggestions on how to improve it?
using UnityEngine;
using System.Collections;
public class PlayerHud : MonoBehaviour {
public int toolbarInt = 0;
private float scrollWheelRaw = 0.2f;
private bool canScroll = true;
private int screenWidth;
private int screenHeight;
GUIContent[] toolbarStrings;
//Used to activate itemscripts
public static bool usingSword;
//GUIContent contains string, image and tooltip
public GUIContent toolbar1;
public GUIContent toolbar2;
public GUIContent toolbar3;
public GUIContent toolbar4;
public GUIContent toolbar5;
public GUIContent toolbar6;
public GUIContent toolbar7;
public GUIContent toolbar8;
public GUIContent toolbar9;
//Used in addItem to check if toolbar is filled
private bool t1IsFilled;
private bool t2IsFilled;
private bool t3IsFilled;
private bool t4IsFilled;
private bool t5IsFilled;
private bool t6IsFilled;
private bool t7IsFilled;
private bool t8IsFilled;
private bool t9IsFilled;
//item types in all slots
private string t1ItemType;
private string t2ItemType;
private string t3ItemType;
private string t4ItemType;
private string t5ItemType;
private string t6ItemType;
private string t7ItemType;
private string t8ItemType;
private string t9ItemType;
void OnGUI()
{
toolbarStrings = new GUIContent[9];
toolbarStrings[0] = toolbar1;
toolbarStrings[1] = toolbar2;
toolbarStrings[2] = toolbar3;
toolbarStrings[3] = toolbar4;
toolbarStrings[4] = toolbar5;
toolbarStrings[5] = toolbar6;
toolbarStrings[6] = toolbar7;
toolbarStrings[7] = toolbar8;
toolbarStrings[8] = toolbar9;
Screen.showCursor = true;
screenWidth = Screen.width;
screenHeight = Screen.height;
GameObject play = GameObject.FindGameObjectWithTag("player");
Player pla = play.GetComponent<Player>();
//healthnumber placement
GUI.Label(new Rect(0, 0, screenWidth, screenHeight), pla.health.ToString());
//toolbar dimensions
toolbarInt = GUI.Toolbar(new Rect(screenWidth / 4, screenHeight - (screenHeight / 12), screenWidth / 2, screenHeight / 12), toolbarInt, toolbarStrings);
//Toolbar selection by scrollwheel
if (Input.GetAxis("Mouse ScrollWheel") > 0 && canScroll == true)
{
StartCoroutine("scrollUp");
}
else if(Input.GetAxis("Mouse ScrollWheel") < 0 && canScroll == true)
{
StartCoroutine("scrollDown");
}
//detect which button the mouse is over
if(GUI.tooltip == "button1" && Input.GetKey(KeyCode.Mouse0))
{
print ("Mouse is over button1");
}
if(GUI.tooltip == "button2" && Input.GetKey(KeyCode.Mouse0))
{
print ("Mouse is over button2");
}
if(GUI.tooltip == "button3" && Input.GetKey(KeyCode.Mouse0))
{
print ("Mouse is over button3");
}
if(GUI.tooltip == "button4" && Input.GetKey(KeyCode.Mouse0))
{
print ("Mouse is over button4");
}
if(GUI.tooltip == "button5" && Input.GetKey(KeyCode.Mouse0))
{
print ("Mouse is over button5");
}
if(GUI.tooltip == "button6" && Input.GetKey(KeyCode.Mouse0))
{
print ("Mouse is over button6");
}
if(GUI.tooltip == "button7" && Input.GetKey(KeyCode.Mouse0))
{
print ("Mouse is over button7");
}
if(GUI.tooltip == "button8" && Input.GetKey(KeyCode.Mouse0))
{
print ("Mouse is over button8");
}
if(GUI.tooltip == "button9" && Input.GetKey(KeyCode.Mouse0))
{
print ("Mouse is over button9");
}
//Toolbar selection by number keys
if(Input.GetKeyDown(KeyCode.Alpha1))
{
toolbarInt = 0;
}
if(Input.GetKeyDown(KeyCode.Alpha2))
{
toolbarInt = 1;
}
if(Input.GetKeyDown(KeyCode.Alpha3))
{
toolbarInt = 2;
}
if(Input.GetKeyDown(KeyCode.Alpha4))
{
toolbarInt = 3;
}
if(Input.GetKeyDown(KeyCode.Alpha5))
{
toolbarInt = 4;
}
if(Input.GetKeyDown(KeyCode.Alpha6))
{
toolbarInt = 5;
}
if(Input.GetKeyDown(KeyCode.Alpha7))
{
toolbarInt = 6;
}
if(Input.GetKeyDown(KeyCode.Alpha8))
{
toolbarInt = 7;
}
if(Input.GetKeyDown(KeyCode.Alpha9))
{
toolbarInt = 8;
}
//Activates script attached to object types
}
//Delay to get 1 output from scrollwheel
private IEnumerator scrollUp()
{
if(toolbarInt < 8)
{
toolbarInt +=1;
canScroll = false;
yield return new WaitForSeconds(0.01f);
canScroll = true;
}
}
private IEnumerator scrollDown()
{
if (toolbarInt > 0)
{
toolbarInt -=1;
canScroll = false;
yield return new WaitForSeconds(0.01f);
canScroll = true;
}
}
public void addItem()
{
crosshairRayInteractor cri = GetComponent<crosshairRayInteractor>();
if (t1IsFilled == false)
{
toolbar1 = new GUIContent(cri.itemNameFromRayScript);
t1ItemType = cri.itemTypeFromRayScript;
t1IsFilled = true;
}
else if(t2IsFilled == false)
{
toolbar2 = new GUIContent(cri.itemNameFromRayScript);
t2ItemType = cri.itemTypeFromRayScript;
t2IsFilled = true;
}
else if(t3IsFilled == false)
{
toolbar3 = new GUIContent(cri.itemNameFromRayScript);
t3ItemType = cri.itemTypeFromRayScript;
t3IsFilled = true;
}
else if(t4IsFilled == false)
{
toolbar4 = new GUIContent(cri.itemNameFromRayScript);
t4ItemType = cri.itemTypeFromRayScript;
t4IsFilled = true;
}
else if(t5IsFilled == false)
{
toolbar5 = new GUIContent(cri.itemNameFromRayScript);
t5ItemType = cri.itemTypeFromRayScript;
t5IsFilled = true;
}
else if(t6IsFilled == false)
{
toolbar6 = new GUIContent(cri.itemNameFromRayScript);
t6ItemType = cri.itemTypeFromRayScript;
t6IsFilled = true;
}
else if(t7IsFilled == false)
{
toolbar7 = new GUIContent(cri.itemNameFromRayScript);
t7ItemType = cri.itemTypeFromRayScript;
t7IsFilled = true;
}
else if(t8IsFilled == false)
{
toolbar8 = new GUIContent(cri.itemNameFromRayScript);
t8ItemType = cri.itemTypeFromRayScript;
t8IsFilled = true;
}
else if(t9IsFilled == false)
{
toolbar9 = new GUIContent(cri.itemNameFromRayScript);
t9ItemType = cri.itemTypeFromRayScript;
t9IsFilled = true;
}
}
public void toolbarChecker()
{
crosshairRayInteractor cri = GetComponent<crosshairRayInteractor>();
if (cri.itemTypeFromRayScript == "sword")
{
usingSword = true;
}
else
{
usingSword = false;
}
}
}