0
\$\begingroup\$

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?

\$\endgroup\$

closed as off-topic by MichaelHouse Apr 7 '15 at 2:35

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – MichaelHouse
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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
1
\$\begingroup\$

It worked after i replaced

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

with

index=System.Array.IndexOf(go,GameObject.Find (temp));
\$\endgroup\$
  • 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.