I’m trying to use a for loop to create a button for every item is found inside the list menuItems. However i also want each button to be 50 pixels away from the last button. To do this i add 50 pixels to the xPos of the button after each loop. the problem is that it keeps adding 50 to it every frame. Help?
var rayLength : float = 30;
private var buttonPosX : int = 10;
private var buttonPosY : int = 10;
var menuItems : Array = [];
var itemTextures : Array = [];
private var currentGameObject : String;
function Update () {
InventoryRay();
//PrintList(menuItems);
}
function PrintList(list) {
Debug.Log(list);
}
function InventoryRay() {
var hit : RaycastHit;
Debug.DrawRay(transform.position, transform.forward * rayLength, Color.red);
if(Physics.Raycast(transform.position, transform.forward, hit, rayLength))
{
if(Input.GetMouseButtonDown(0))
{
if(hit.collider.gameObject.tag == "collectable")
{
Debug.Log("collectable");
currentGameObject = hit.collider.gameObject.name;
Debug.Log(currentGameObject);
menuItems.Add(currentGameObject);
}
}
}
}
function OnGUI() {
GUI.Box(Rect(0, 0, Screen.width/2, Screen.height/2), "Inventory");
for(items in menuItems)
{
if(GUI.Button(Rect(buttonPosX, buttonPosY, 100, 100), items))
{
}
buttonPosX = buttonPosX + 50;
}
}