# Find index of GameObject in array of GameObjects [closed]

In my script, i have declared an array of GameObjects like this:

public GameObject[] go;ld object name. String temp;


Also, i have a temporary variable to ho Then, i assigned objects to elements in Inspector.

I'm using Raycasting to find which object was clicked. Block of code for handling Raycast:

Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast (ray.origin, ray.direction, Mathf.Infinity,layerMask);

if (hit) {
temp=hit.collider.gameObject.name;
index=ArrayList.IndexOf(go,GameObject.Find (temp));

}


Ray casting works well. It will print object name if i insert a Debug.Log. However, what i want to do is, find the GameObject's index in the array "go". I need this index because i have another boolean array isSelect[] which, has a property that needs to be set to true when the corresponding GameObject is selected.

With the IndexOf code above, i get the following error:

An object reference is required to access non-static member System.Collections.ArrayList.IndexOf(object)


What changes do i need to make to this code?

• fyi doing GameObject.Find() here is pretty wasteful, since you already had a reference to the gameobject. That's what hit.collider.gameobject is, no need to search for it by name – jhocking Dec 6 '14 at 11:50
• You are right i should instead try something like ' index=System.Array.IndexOf(go,hit.collider.GameObject);' – SanSolo Dec 6 '14 at 12:02

index=ArrayList.IndexOf(go,GameObject.Find (temp));

index=System.Array.IndexOf(go,GameObject.Find (temp));
`