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.