Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Nullitifullness · Apr 18, 2016 at 01:31 PM · arrayscollision2darray of gameobjectsindex

How to find index of colliding Game Object in OnCollisionEnter2D()

I have created a prefab and instantiated it a number of times in a script that it attached to another game object as below.

 void Start () {
 
     badGuys= new List<GameObject> ();
 
     int numberOfBadGuys = 6;
     Camera camera = GameObject.FindGameObjectWithTag ("MainCamera").GetComponent<Camera> ();
 
     for (int i = 1; i < numberOfBadGuys + 1; i++) {
         GameObject badGuyObject =  (GameObject)Instantiate(badGuy, new Vector3(Screen.width*i/2, Screen.height*i/6, camera.nearClipPlane ), Quaternion.identity );
         badGuys.Add(badGuyObject);
     }
 
 }
 

Since all of the instantiated objects in the array have the same tag and game object, how can I find the index of the colliding object in the array?

 void OnCollisionEnter2D(Collision2D col)    {
     Debug.Log("collision has began");
 
     if (col.gameObject.tag == "badGuy") {
              // how can I tell the index of colliding game object in badGuys array
       }
 }
 

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by thereisnotrying · Apr 19, 2016 at 02:46 AM

I don't think you can. It is the list that references GameObjects not the other way around. So having a reference of that GameObject does not let you reveal any information hold by the list.

However, you could add an int variable "creation number" cn. This would require a script.

  for (int i = 0; i < numberOfBadGuys; i++) {
      GameObject badGuyObject =  (GameObject)Instantiate(badGuy, new Vector3(Screen.width*i/2,      Screen.height*i/6, camera.nearClipPlane ), Quaternion.identity );
      //new line
      badGuyObject.GetComponent<YourScript>().cn = i;
      badGuys.Add(badGuyObject);
  }

So later you could access it later:

 if (col.gameObject.tag == "badGuy") {
               // how can I tell the index of colliding game object in badGuys array
              int index = col.gameObject.GetComponent<YourScript>().cn;
        }

If you want this index to interact with your bad guys objects to take away health or perform other operations remember that you already have access to the object itself. So if this would be collision script for a rocket flying into bad guys you could just code something like this:

 if (col.gameObject.tag == "badGuy") {           
               col.gameObject.GetComponent<YourScript>().HitByRocket():
        }


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
avatar image
0

Answer by jgodfrey · Apr 18, 2016 at 10:20 PM

Compare the id InstanceID of the collided object to the InstanceID's of the cached objects until you find a match?

http://docs.unity3d.com/ScriptReference/Object.GetInstanceID.html

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone tell me what is wrong with this? 2 Answers

How to find the index of an Object in an Array 2 Answers

Obtaining the Variable of a Prefab in an array. 1 Answer

Calling Game object from Element list? 0 Answers

Changing the material of all children, then change back to old one 1 Answer


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