[ANSWERED] IndexOf and LastIndexOf both returning -1 when an item is on the list.
Ive been working on a simple inventory system in unity, and this problem has been driving me nuts for a couple days now
i have a very simple inventory system that stores the transforms of various inventory icons in a list, and i am trying to code a "drop" function that will remove specific items from the list, so that they no longer appear in the inventory.
I have tried using both "IndexOf" and "LastIndexOf" and both of them are returning -1 as if the item was not listed, but as far as i can tell, the item is definitely listed.
here is some sample code
//transform that was previously added to list
public Transform thistrans;
//attempting to find the index of said item
itemlist = invmanager.GetComponent<ItemLists>();
toRemove = itemlist.weaponimages.LastIndexOf(thistrans);
print (toRemove);
if anyone wants additional info please ask, thanks.
Answer by Vonricter · May 14, 2017 at 08:26 AM
I figured this out after some more investigation
the problem was the editor was unable to tell the difference between the prefab item and the active item in the hierarchy, and was trying to find the list location of the item in the hierarchy, which technically wasn't listed.
to solve this i simply created another game object to store the prefabbed icon, and listed that object instead.