Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

closed as off-topic by Byte56 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?"" – Byte56
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
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
up vote 1 down vote accepted

It worked after i replaced

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

with

index=System.Array.IndexOf(go,GameObject.Find (temp));
share|improve this answer
1  
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. – jhocking Dec 11 '14 at 12:57

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