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) {
            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?

  • 1
    \$\begingroup\$ 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 \$\endgroup\$ – jhocking Dec 6 '14 at 11:50
  • \$\begingroup\$ You are right i should instead try something like ' index=System.Array.IndexOf(go,hit.collider.GameObject);' \$\endgroup\$ – SanSolo Dec 6 '14 at 12:02

It worked after i replaced

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


index=System.Array.IndexOf(go,GameObject.Find (temp));
| improve this answer | |
  • 1
    \$\begingroup\$ So in other words the returned object is an Array not an ArrayList. The names are similar (indeed, the object's are similar) but they aren't the same. \$\endgroup\$ – jhocking Dec 11 '14 at 12:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.