- Home /
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[i].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: $$anonymous$$onoBehaviour {
void On$$anonymous$$ouseDown()
{
//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.
Answer by clunk47 · Jul 29, 2013 at 10:35 PM
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[i].gameObject)
{
label = "Your cube's index in the array is: " + i.ToString();
}
}
}
}
void OnGUI()
{
GUILayout.Label(label);
}
}
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.
Well it doesn't have to be attached to the main camera. It can be attached really to anything, because it's calling the main camera no matter with Camera.main, which grabs the camera with tag $$anonymous$$ainCamera. I just said to do so because it makes it easier (for me anyway). I have edited the code to use GUI ins$$anonymous$$d of the console. Try the revised code in my answer.
Answer by rhys_vdw · Jul 29, 2013 at 08:59 AM
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>();
Your answer
Follow this Question
Related Questions
Calling Game object from Element list? 0 Answers
How can I change the index of an array object? 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers