Hi all! Here i go! I got a inventory array where i put my item name in it once they've been picked up! Then i want to create a button for each items listed in this array with a for each loops.
Problem is the loop is working but it put all the content of the inventory in one button! How can i create a button for each item. I've also tried with a regular for loop. So anyway here my code :
function OnGUI ()
{
//Show the content of the inventory and convert it to string
for(i in InventoryManager.inventory)
{
if(GUI.Button (Rect (25, 25, 150, 30), InventoryManager.inventory.ToString()));
}
}
You just gave us a small piece of your code. We don't know what's inside your inventory enumeration. I just guess it's a string. In that case it should look like that:
function OnGUI ()
{
//Show the content of the inventory and convert it to string
for(i in InventoryManager.inventory)
{
if(GUI.Button (Rect (25 + (i * 150), 25, 150, 30), InventoryManager.inventory.ToString()))
{
// Place your function
print("You have selected " + InventoryManager.inventory.ToString());
}
}
}
The GUI Button will be spawning on top of one another, you should increment one of the rect variables to move the box as you place your inventory. Example:
var yOffset = 0.0f;
foreach(Inventory i in InventoryManager.inventory)
{
if(GUI.Button (Rect (25, (25 + yOffset), 150, 30), InventoryManager.inventory.ToString()))
yOffset += 30;
}
This will stack the gui boxes on top of one another.
I've tried both codes and it's doing the same thing both! It's creating only one button with all the inventory in it! Also just for the record, the array inventory is an array that i've declared just like that : inventory = new Array (); //declaring the inventory
What i'm not understanding both of the code up there should works and I tougth the same that it was the offset the problem!! But it's it's creating only one instance of the buttons!
By the thanks ya all for the help!
I'm giving you all the sample of the code here :
InventoryManager
static var inventory : Array; //Starting the inventory
var GuiPrefab : GameObject;
var guiState : boolean = false;
function Awake ()
{
inventory = new Array (); //declaring the inventory
}
function Update ()
{
CheckInventory();
}
////////////////////////////////////Additionnal Function/////////////////////////////////////
//Function to show the inventory and what in it!
function CheckInventory()
{
if(Input.GetKeyDown("i") && guiState==false)
{
var GuiInventory = Instantiate(GuiPrefab,transform.position,transform.rotation);
guiState=true;
}
else if(Input.GetKeyDown("i") && guiState==true)
{
Destroy(GameObject.FindWithTag("GUI"));
guiState=false;
}
}
Pickup Script
var initialColor : Color; //initailise the variable for the initale color for the mouse over.
function Awake ()
{
initialColor = gameObject.renderer.material.color;//initailise the variable for the initale color for the mouse over.
}
function OnMouseUp ()
{
if(gameObject.FindGameObjectsWithTag("Item")) //if the object is an item you can pickit up
{
print("You've picked the object");
Destroy(gameObject); // Get rid of the object
AddToInventory(); //call the function to put item in inventory
}
}
function AddToInventory()
{
print("adding this object to the inventory: " + gameObject.name); //tell you what happened
InventoryManager.inventory.Push(gameObject.name); //get acces to the inventory array from InventoryManager and put it in it!
}
function OnMouseOver ()
{
if(gameObject.FindGameObjectsWithTag("Item"))
{
print("This is an item");
gameObject.renderer.material.color = Color.red; //higligth the object so we know you can pickit up
}
}
function OnMouseExit ()
{
if(gameObject.FindGameObjectsWithTag("Item"))
{
print("Getting out of item");
gameObject.renderer.material.color = initialColor; //on the mouse exit return it to it inital texture;
}
}
GuiInventory
function OnGUI()
{
var yOffset = 0.0f;
for(i in InventoryManager.inventory)
{
if(GUI.Button (Rect (25, (25 + yOffset), 150, 30), InventoryManager.inventory.ToString()))
yOffset += 30;
}
}
Well thanks for all the help the solution from Bunny83 with the layout works really great!!!
Since i'm a beginner im working my way steps by steps for the inventory system im working on!
Next step will be to add small icons textures instead of name and then add the drag and drop behavior!
I found a lot of inventory script either on the wiki or in the forum but thoses are really ways complicated for my needs!! :)
Anyway there's not better way to learn than doing it yourself!
Thanks for the help all! The community here so great!