cannot convert `UnityEngine.GameObject' expression to type `System.Predicate< UnityEngine.GameObject >'

c# script

private GameObject GameObjSeen = new GameObject[50];

void Update()

{
	foreach(GameObject Obj in GameObjSeen)
	{
		if(Vector3.Distance(Obj.transform.position,transform.position) > 60)
		{
			int i3;
			i3 = System.Array.FindIndex(GameObjSeen,Obj);
			GameObjSeen[i3] = null;
		}
	}
}

unity told me that:

Argument #2' cannot convert UnityEngine.GameObject’ expression to type `System.Predicate< UnityEngine.GameObject >’

and that:

The best overloaded method match for `System.Array.FindIndex(UnityEngine.GameObject, System.Predicate)’ has some invalid arguments

what should i try?

replace i3 = System.Array.FindIndex(GameObjSeen,Obj as GameObject); with i3 = GameObjSeen.indexOf(Obj);

1 Answer

1

You need to specify a predicate function as either a method or a lamda.

I guess it would look something like:

    i3 = GameObjSeen.FindIndex(o=>o == Obj);

But it would be easier and much faster to just do a for…next loop or keep an incrementing loop counter in your foreach loop.

i3 = System.Array.FindIndex(GameObjSeen,o => o== Obj); worked

Ah yeah sorry - forgot it was a static :S