- Home /
ArrayList indexOf always returns -1
So I have a script attached to a parent object which has 4 children objects; the script has a class (zombie_object_list) which runs a function to create an ArrayList (zombie_objects) from the child objects which are marked as "zombie_mesh". This array list is suppose to have a count of 4.This part works fine.I get the count 4.
Now in this script I assaign a random int to "zmb_meshname" ittarate through my arraylist and then try to use indexOf to check if the zmb_meshname equals one of the arraylist objects but I always get -1 as result ( means null).
I dont get where the problem is.. Thanks for the help.
public void zombieRandomize()
{
// assaign zombie enemy a random zombie object number(0-3).
int zmb_meshname = Random.Range(0,zombie_ObjectList.zombie_objects.Count-1);
int zmb_health = Random.Range(50,100);
int zmb_speed = Random.Range(3,10);
for(int i = 0 ; i < zombie_ObjectList.zombie_objects.Count ; i++)
{
if(zmb_meshname == zombie_ObjectList.zombie_objects.IndexOf(zmb_meshname))
zombie_ObjectList.gameObject.SetActive(true);
else if (zmb_meshname != zombie_ObjectList.zombie_objects.IndexOf(zmb_meshname))
zombie_ObjectList.gameObject.SetActive(false);
Debug.Log("ZMB_MESHNAME : " + zmb_meshname.ToString() + " INDEX : " + zombie_ObjectList.zombie_objects.IndexOf(zmb_meshname));
Debug.LogWarning("INDEX LOOKING AT GAMEOBJECT : " + zombie_ObjectList.zombie_objects);
}
}
Answer by gjf · Jul 11, 2014 at 10:34 AM
seems like you don't need IndexOf
at all - just compare zmb_meshname
(that's a bad choice of variable name btw because it's not a name, but a number) with the loop index i
you could simplify the test within the loop with the following:
zombie_ObjectList.gameObject.SetActive(zmb_meshname == i);
and your loop count needs a -1 :
for(int i = 0 ; i < (zombie_ObjectList.zombie_objects.Count - 1) ; i++)
Thanks for the answer but, it is still -1 in any condition; the SetActive(bool) just sets the bool to true or false for the result of (zmb_meshname == i) but it returns false every time.
And if I add -1 to loop count it will only loop within 3 members or arraylist. so it has to be like without -1 in my opinion.
if(zmb_meshname == zombie_ObjectList.zombie_objects.IndexOf(i)); also returns false. Debug says they are all -1.
PS: I know the zmb_meshname is a bad name it just a placeholder please ignore it.
list/array index start at zero, so usually we iterate from zero to count - 1
what is the value of zmb_meshname
?
the value of zmb_meshname is assaigned randomly between range 0 and arraylist.count-1 . 0,1,2,3 . On line 5
However, I think I have found the problem and I have made a mistake on the other script, it seems the Array list only consist of 1 object added 4 times. Since this object returns -1 then all the 4 elements of the arraylist return -1.
thanks again. Will update when I fix the issue and post an answer.
i can see from your code where it was assigning. i should have been clearer regarding why i was asking. the answer should have stated that adding a Debug.Log()
after zmb_meshname
is assigned will display the number so that you can manually check whether the expected item exists or not - obviously, it's not going to work if your list doesn't have the proper values!! ;)
my suggestion about not needing IndexOf()
, count-1 and the single line assignment using SetActive()
are all valid. your code will be so much cleaner, easier to understand and modify in the future...