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 TifaOng · Oct 01, 2013 at 08:05 AM · c#colliderarray

Storing collided gameObject array index

Hi Everyone,

I have 3 gameObject players in array.

 public GameObject [] player;
 player = GameObject.FindGameObjectsWithTag ("PlayerBlue");
 
 if (collision.gameObject.tag == "PlayerBlue")
 {
     i = (store array index here)
     transform.parent = player[i].transform;
 }

How do I store the index of collided game object? I want to make collided player to be the parent of the ball.

Please advise. Thank you.

Comment
Add comment · Show 3
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 ShadoX · Oct 01, 2013 at 10:45 AM 1
Share

Hi, I'd like to suggest not to use the Find method.. I remember someone from Unity stating that it's a resource hog and that you should most definitely figure out a different way of doing the same task.

Personally I just check the gameObject name (gameObject.name) because it won't change (in my case). As for the array. I'm not sure what exactly you mean by index. You could try storing information about the transform in an array in case if you plan on referencing that info later on.

It sounds a lot like a footBall/Soccer game so, why not just attach the script to the ball and change the parent of the object on Collision? Something like...

 OnCollisionEnter(Collision obj){
    transform.parent = obj.transform;
 }
avatar image TifaOng · Oct 02, 2013 at 02:37 AM 0
Share

Hi Shadox,

Thanks for the info. This script is attached to Ball and indeed this is a soccer game >.<

I tried your method and it works. But for future, the ball will only become child of player. For your method, if ball collide with a wall, the wall will become parent of the ball which does not make sense.

Thanks for the info!

avatar image ShadoX · Oct 02, 2013 at 08:48 AM 0
Share

Oh, sorry about that, didn't think of that.

You could try to mark gameObjects in some way. Like some variable that , if it exists or is set to some specific value (or the gameObject has some specific name or namePattern) means that it's a character on the field and not a wall or something else.

But from the looks of it you already got your answer, so.. :)

1 Reply

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

Answer by mattssonon · Oct 01, 2013 at 11:45 AM

You could do it like this:

 if (collision.gameObject.tag == "PlayerBlue")
 {
     for (int j = 0; j < player.Length; j++) {
         if (player[j] == collision.gameObject) {
             i = j;
             transform.parent = player[i].transform;
             break;
         }
     }
 }

We compare the tagged game objects with the collision game object and save it if it's the right one.

Comment
Add comment · Show 1 · 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 TifaOng · Oct 02, 2013 at 02:50 AM 0
Share

This works like a charm! Thanks a lot $$anonymous$$att!

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

16 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

Related Questions

[C#] Buttons through an Array. 1 Answer

A problem with intersection detection 1 Answer

Array empties values on RunTime? 1 Answer

Selecting Object From Top Causes NullReference 1 Answer

Index out of Range Exception Error 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