- Home /
Inventory GUI help.... (click for more info)
So, i'm making a inventory system in unity using javascript. And the part where the buttons are i'm trying to make them not appear on the same spot, i want 4 in a row and then another row with 4 under. So, i made 2 var's one yOffset and the other xOffset, so everytime the for loop loops trough all the items, it makes a button, and then checks if it has looped 4 times, which is has not, so it only adds a number to the xOffset. But the buttons are still on the same spot. Why? Code :
var inventory = Array("food", "water", "test", "All those after me, including me should be on the first row", "this should be on the 2nd row, first slot"); //The current inventory.
var inventoryOpen : boolean = false; //Checks if the player opened the inventory using the "E" button.
var xOffset : int = 10; //Offset of the item buttons, LEFT / RIGHT
var yOffset : int = 10; // UP / DOWN
var timesLooped : int = 0; //So the buttons dont respawn all the time.
function Start()
{
}
function Update()
{
if (Input.GetKeyDown(KeyCode.E) && inventoryOpen == false)
{
inventoryOpen = true; //Resets everything back to normal so the Inventory can eb displayed
xOffset = 10;
yOffset = 0;
timesLooped = 0;
}
else if (Input.GetKeyDown(KeyCode.E) && inventoryOpen == true)
{
inventoryOpen = false;
}
}
function OnGUI()
{
if (inventoryOpen == true)
{
if (timesLooped < inventory.length)
{
for (i in inventory)
{
GUI.Button(Rect(xOffset, yOffset, 50, 50), i);
if (timesLooped > 1) //Makes so the first button doesn't move since its already in its spot.
{
timesLooped += 1;
xOffset += 10;
if (timesLooped > 5 && timesLooped < 6) //if theres is 4 buttons on the same row, it will add the next one on the 2nd row.
{
xOffset = 0;
yOffset = 60;
}
else if (timesLooped > 10 && timesLooped < 11) //same but on 3rd row.
{
xOffset = 0;
yOffset += 60;
}
else if (timesLooped > 20 && timesLooped < 21) //same but on 4th row.
{
xOffset = 0;
yOffset += 60;
}
}
}
}
}
}
// Made by "HappyCrayfish" 2015.
Answer by WoozyBytes · Apr 14, 2015 at 05:27 PM
Hi there!!!
I think you are overcomplicating all you need is something like this:
var numOfButtonsPerRow : int = 4;
var numOfRows : int = 2;
var btnWidth : int = 64;
var btnHeight : int = 64;
function OnGUI ()
{
for( var rowN = 0 ; rowN < numOfRows; rowN ++)
{
for( var btnN = 0 ; btnN < numOfButtonsPerRow; btnN++)
{
if( GUI.Button(Rect( btnN * btnWidth , rowN * btnHeight , btnWidth,btnHeight),"Button"))
{
Debug.Log("Working");
}
}
}
}
Hope It Helps
I literarly forgot my password for this account until i reazlied i wrote it up (today)... Sorry for late reply. Will try this right away! Edit : So, this worked.... I can't really understand how it's not just moving ALL buttons though... Umm, could you maybe explain to me? :P Thank you for helping me :) Edit 2 : So, umm... What if i want the inventory to be an array so i can add more items and it will automatically add a new button?
Edit : So, this worked.... I can't really understand how it's not just moving ALL buttons though... Umm, could you maybe explain to me?
Hi @nobx101 what do you mean by moving all Buttons ?
If you mean position all buttons you can create 2 public variables that you can adjust in the inspector one for X Axis(Left/Right)and one for the Y axis(Up/Down) and add after btnWidth and after btnHeight respectively.
For Example
var numOfButtonsPerRow : int = 4;
var numOfRows : int = 2;
var btnWidth : int = 64;
var btnHeight : int = 64;
var offSetX : int = 0; // Adjust this in inspector
var offSetY : int = 0; // Adjust this in inspector
function OnGUI ()
{
for( var rowN = 0 ; rowN < numOfRows; rowN ++)
{
for( var btnN = 0 ; btnN < numOfButtonsPerRow; btnN++)
{
if( GUI.Button(Rect( btnN * btnWidth + offSetX, rowN * btnHeight + offSetY , btnWidth,btnHeight),"Button"))
{
Debug.Log("Working");
}
}
}
}
Edit 2 : So, umm... What if i want the inventory to be an array so i can add more items and it will automatically add a new button?
What's the max number of slots you want in the inventory?
you can set the max number of inventory slots lets say for example 20, now at first the user might have access to only 10 slots so you just disable the other 10 buttons.
var maxSlots : int = 20; // $$anonymous$$ax Number of Slots
var allowedNumberOfSlots : int = 10; // Size of your array
private var curSlot : int= -1 ; // this is to count the buttons
var numOfButtonsPerRow : int = 5;
var numOfRows : int = 4;
var btnWidth : int = 64;
var btnHeight : int = 64;
var offSetX : int = 0; // Adjust this in inspector
var offSetY : int = 0; // Adjust this in inspector
function OnGUI ()
{
for( var rowN = 0 ; rowN < numOfRows; rowN ++)
{
for( var btnN = 0 ; btnN < numOfButtonsPerRow; btnN++)
{
curSlot++;
if( GUI.Button(Rect( btnN * btnWidth + offSetX, rowN * btnHeight + offSetY , btnWidth,btnHeight),"Button"))
{
if( curSlot < allowedNumberOfSlots )
Debug.Log("Working");
else
Debug.Log("Disabled");
}
}
if( curSlot == maxSlots - 1 ) // $$anonymous$$us 1 because we start counting at zero
curSlot = -1 ;
}
}
Is This what you need ?
if You don't want the buttons to show up when disabled change this in the code above:
if( GUI.Button(Rect( btnN * btnWidth + offSetX, rowN * btnHeight + offSetY , btnWidth,btnHeight),"Button"))
{
if( curSlot < allowedNumberOfSlots )
Debug.Log("Working");
else
Debug.Log("Disabled");
}
To This:
if( curSlot < allowedNumberOfSlots )
{
if( GUI.Button(Rect( btnN * btnWidth + offSetX, rowN * btnHeight + offSetY , btnWidth,btnHeight),"Button"))
{
Debug.Log("Working");
}
}
Regards
Your answer
Follow this Question
Related Questions
How I can make a simple follow AI in Unity 5? 1 Answer
Monodevelop freezes opening js files after creating new project 1 Answer
Upgrading to unity 5... lots of errors... please help. 1 Answer
Simple go forward and turn back loop movement stuck to the limit without reverse 2 Answers
No animation attached to gameobject? 2 Answers