Identify an array index of a gameobject when clicked and interact with the selected gameobject(index) individually

I wanted to obtain the index of a gameobject when clicked and interact with the selected gameobject. I Instantiate a prefab using another script “myGUI” using the GUI button… When the GUI button is pressed and I click on the terrain… the prefab is then instantiated on the position of the input.mousePosition on the terrain. How do I find the index of a gameobject and interact with it individually when multiple prefabs of the same game object are instantiated.

Here’s a sample Code in an attempt to find the index:

    public class IndexSelect : MonoBehaviour {
    	//Array list for identifying indexes
    	static ArrayList MovePlayer = new ArrayList();
    	int i;
    	//End Array list
    
    	RaycastHit hit;
    	public Light myLight;
    	public bool Selected = false;
    
    
    
    		// Use this for initialization
    	void Start () {
    
    	
    	}
    	// Update is called once per frame
    	void Update () {
    	    Ray ray = camera.ScreenPointToRay(Input.mousePosition);
            //Debug.DrawRay(ray.origin, ray.direction * 100, Color.green);
    		
    		 if(Physics.Raycast(ray, out hit, 100))
    			{
    			print ("Here is an object!");
    			if (hit.transform.gameObject.tag == ("Ground")){
    				//Debug.DrawRay(ray.origin, ray.direction * 100, Color.red);
    				print ("Ray has Hit the Ground");
    			}
    			else if (hit.transform.gameObject.tag == ("Player")){
    				Debug.DrawRay(ray.origin, ray.direction * 100, Color.blue);
    				if ((Input.GetMouseButtonDown(0))&&(hit.transform.gameObject.tag == "Player")){
    					print("You Clicked the Player!");
    					GameObject[] light = GameObject.FindGameObjectsWithTag("Player");
    					myLight = light*.GetComponent<Light>();*
  •  			myLight.enabled = true;*
    
  •  			Selected = true;					*
    
  •  		}*
    
  •  	else*
    
  •  	{*
    
  •  	}*
    
  • }*
    }

You can think another way, example you can attach a simple script like that to the object tag "Player" public class PlayerClick: MonoBehaviour { void OnMouseDown() { //this function is called whenever the mouse is over the object gameObject.light.enable = true; } } And there are many ways to get the Selected variable set to true.

2 Answers

2

Ok so let’s say you have 3 cubes in your scene. Each has the tag “Cube”. This will print the index of each cube when you click on it.

EDIT: If you want to call the specific gameObject, just use cubes[0], cubes[1], cubes[2] and so on.

using UnityEngine;
    using System.Collections;
    
    public class Example : MonoBehaviour 
    {
    	GameObject[] cubes;
    	Ray ray;
    	RaycastHit hit;
		string label = "Click on a cube to get the index of its gameObject in the array.";
    	
    	void Update()
    	{
    		cubes = GameObject.FindGameObjectsWithTag ("Cube");
    		ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    		if(Physics.Raycast(ray, out hit) && Input.GetMouseButtonDown(0))
    		{
    			for(int i = 0; i < cubes.Length; i++)
    			{
    				if(hit.collider.gameObject == cubes*.gameObject)*
  •  		{*
    
  •  			label = "Your cube's index in the array is: " + i.ToString();	*
    
  •  		}*
    
  •  	}*
    
  •  }*
    
  • }*

  •  void OnGUI()*
    
  •  {*
    
  •  	GUILayout.Label(label);*
    
  •  }*
    

}

I tested the code but it doesn't seem to work. I made a few objects with Cube as the tag and when i click the tagged game object; nothing really happens. I don't get to see the index value at all. I also have a compiler error that says UnityEngine.GameObject[] does not contain a definition for 'length' and no extension method 'length' of type. (AKA ERROR CS1061 on line 17)

It doesn't print to the console?

This should be attached to your main camera or an empty game Object.

Working perfectly on my end. I'll edit to show the index on GUI.

As it says in your code ray comes from the camera so yeah... I attached the script to the main camera

You don’t need to access the array. You can get a reference to the object directly by using:

hit.transform.gameObject;

So:

if (Input.GetMouseButtonDown(0) && hit.gameObject.tag == "Player"){
    print("You Clicked the Player!");
    myLight = hit.transform.gameObject.GetComponent<Light>();
    myLight.enabled = true;
    Selected = true;             
}

You don’t need your index variable at all.

Hope that all makes sense.

###Other notes:

you don’t need to store your RaycastHit outside of the Update function.

It’s preferable to use the generic List<> container than ArrayList. You can get this from System.Collections.Generic and would be used like this:

List<GameObject> gameObjects = new List<GameObject>();