- Home /
Add GameObjects to an Array List on RayCastHit
How do I do I add to an array? I have been looking through everything I can find and I still can't figure this out. When I add the item to my array it removes the "picked up" object from my array and replaces it with the most currently picked up item. Any help with this would be greatly appreciated I'm going crazy trying to figure this out.
Some screen shots to clarify. I want to add the axe to the array not the old chest. (Sorry they're so dark they were't when I took them. The crosshair is looking at a chest)
Answer by Landern · Dec 05, 2012 at 09:02 PM
EDIT #3:
I thought that lootID1 was a list of gameobject, i've updated to fix it.
END EDIT #3
Okay, lets try this again, i'm going to assume a loot container COULD maybe have more then one item, either way...:
if (Physics.Raycast(ray, hit, 2.5))
{
nameHolder = hit.collider.gameObject.name;
if (Input.GetButtonDown ("Fire1"))
{
if (hit.transform.gameObject.GetComponent(AddToLoot))
{
var lootContainer:AddToLoot = hit.transform.gameObject.GetComponent.<AddToLoot>(); // Get a reference to the AddToLoot object
// storedLootFixedArray should be instantiated in start or when it is declared.
if (storedLootFixedArray == null)
storedLootFixedArray = new List.<GameObject>();
storedLootFixedArray.Add(lootContainer.lootID1); // add references to items to new list
lootContainer.lootTaken = true;
}
}
}
else
nameHolder = "";
I should have explained better.
The lootID1 is from the script AddToLoot. lootID1 holds a gameobject. When the raycast hits a gameobject (Example: a barrel) with the script AddToLoot it gets the lootID1 gameobject I've added (which is an axe) and adds it to the array which is like a temporary inventory. Everything works fine but when I add a new item the array resets and only keeps the newest added item ins$$anonymous$$d of adding it to the array.
lootTaken = true; is so that the player can't get the loot more than once. Once lootTaken is true the item is taken and cannot be taken again.
Does this make sense? I will try the changes you made and see if it works thanks.
I tested this out and it's adding to the array but not properly.
I need it to add the lootID1 gameobject I added via the other script (example: Battle Axe). What your version is doing is adding the gameobject that's holding the script (example: Barrel, Old Chest, Crate)
The barrel has the script AddToLoot I want to get the lootID1 gameobject not the barrel.
Thanks I appreciate the help.
i was assu$$anonymous$$g that lootID1 was not a gameobject givent he field/property name.
thanks, updated answer, also assumed it's possible that your lootID1 list of gameobjects may be more then one item.