- Home /
I'm having trouble taking a game object from an array and storing it in a variable
So I've setup an array for a very basic inventory for three items. The idea is if a player hits 1,2 or 3 on the keyboard then the program loops through the array, finds the object and should store it in the variable. It's not working I'm having a hell of a time figuring this out.
Here is my code:
var inventory = new Array();
var index : int = 0;
var equipped : GameObject;
//this is a bit more complicated, but else if is an example of the structure for the different items
function OnTriggerEnter(collider : Collider)
{
else if(interaction.tag == "item" && interaction.name == "scroll1")
{
inventory[index] = interaction.gameObject;
index++;
interaction.gameObject.collider.enabled = false;
interaction.gameObject.GetComponent(MeshRenderer).enabled = false;
}
}
//records button pressed and passes variable to the CheckInventory function
function Controls()
{
var keyboard : boolean;
if(Input.GetButtonDown("item1"))
{
keyboard = Input.GetButtonDown("item1");
CheckInventory(keyboard);
}
else if(Input.GetButtonDown("item2"))
{
keyboard = Input.GetButtonDown("item2");
CheckInventory(keyboard);
}
else if(Input.GetButtonDown("item3"))
{
keyboard = Input.GetButtonDown("item3");
CheckInventory(keyboard);
}
}
//this function is supposed to store an item from inventory in the equipped variable, it's storing the variable, although the Debug.Log shows up in the console when I hit a key
function CheckInventory(button : boolean)
{
var item : GameObject;
for(item in inventory)
{
if(item.name == "wand1" && button == Input.GetButtonDown("item1"))
{
equipped = item;
Debug.Log("fire");
}
else if(item.name == "scroll1" && button == Input.GetButtonDown("item2"))
{
equipped = item;
Debug.Log("green");
}
else if(item.name == "scroll2" && button == Input.GetButtonDown("item3"))
{
equipped = item;
Debug.Log("electric");
}
}
}
Answer by iwaldrop · Dec 01, 2013 at 10:11 PM
This code is way more complicated than it needs to be, and I'm surprised it even compiles. I guess unityscript is more relaxed, but it shouldn't be.
In your CheckInventory method you declare item to be empty before your loop. Take that line out. Also, why are you passing a boolean to it? You don't use it. The entire method is pointless, and effectively doubles the work that you're already doing in your Controls method. Why don't you just set an index number based off of which button is pressed that will reference the given item when needed? Why are you checking for a button input 3 times before doing anything with it? There is no benefit, and it only slows things down.
var activeIndex : int = 0;
function Controls()
{
if(Input.GetButtonDown("item1"))
activeIndex = 0;
else if(Input.GetButtonDown("item2"))
activeIndex = 1;
else if(Input.GetButtonDown("item3"))
activeIndex = 2;
}
function GetActiveInventoryObject() : GameObject
{
return inventory[activeIndex];
}
well good sir I'm just learning at this point thanks for the program$$anonymous$$g lesson!
I thought a bit about this and realized my code was a tangled mess, and with that you didn't understand what it did.
First the booleans were used; wacky old javascript allowed me to set the data types of the variables to anything I want to, so the Input buttondown was saved in the keyboard variable and passed to the 2nd function to check which key was pressed.
Second I didn't know what order items would go into the array so I didn't think indexes alone would work, I decided to iterate over the array of GameObjects with a GameObject type variable and pull out the one that matched the name I wanted to store in equipped based on button variable.