Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tatsunomi · Jul 29, 2013 at 05:54 AM · c#indexarraylistindexing-array

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
             {
             }
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nixcs2512 · Jul 29, 2013 at 07:32 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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);
         }
     }
     
 
Comment
Add comment · Show 13 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image clunk47 · Jul 31, 2013 at 02:59 AM 1
Share

It doesn't print to the console?

avatar image clunk47 · Jul 31, 2013 at 02:59 AM 1
Share

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

avatar image clunk47 · Jul 31, 2013 at 03:00 AM 1
Share

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

avatar image clunk47 · Jul 31, 2013 at 03:05 AM 1
Share

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.

avatar image clunk47 · Jul 31, 2013 at 03:05 AM 1
Share

Also be sure your script has the same name as the class.

Show more comments
avatar image
1

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>();
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Javascpt Array to c# Arraylist (shift and pop) 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges