• 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 /
This question was closed Jul 02, 2013 at 09:17 AM by Eugenius for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by Eugenius · Jun 25, 2013 at 06:32 PM · javascriptcollisioncolliderarrays

Find colliders after collision?

I know it might sound a bit redundant but I need t$$anonymous$$s for my current game.

Does anyone have any idea how can I return an array of colliders that well, collide?

Basically I have an object that collides with walls, at a certain point I want to know the number of walls it collides with.

FindObjectsOfType(Collider) doesn't seem to work as it returns all the objects in the scene that have a collider. I would need somet$$anonymous$$ng like t$$anonymous$$s that could work simply for when a tag is $$anonymous$$t.

EDIT: To be more clear, I'm trying to find the colliders that I'm currently colliding with.

EDIT2: To be even more specific, I only need to count the colliders that I'm colliding with as I need an action to happen when a certain number of colliders of one tag and a number of colliders with another tag is reached.

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 iwaldrop · Jun 25, 2013 at 06:34 PM 0
Share

Your question is not clear; what colliders are you trying to access? All colliders currently being collided with, or all colliders that have been collided with?

avatar image InfiniBuzz · Jun 25, 2013 at 07:26 PM 2
Share

I'm not sure if I got your question but: On the script you have on the object add the OnCollisionEnter-Method. It will be called each time your object starts collidiong with another collider.

If you want to count collisions you can simply increment a variable in this method.

If you want to save the collider for later use you can add the collider (which is passed as parameter in the OnCollisionEnter function) in a list you declared earlier.

avatar image Eugenius · Jun 25, 2013 at 08:03 PM 0
Share

That wouldn't work, as I said I want to know the number of colliders colliding with my object at the same time as this is going to occur a lot.

2 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by iwaldrop · Jun 25, 2013 at 07:47 PM

Keep a list of colliders. When OnCollisionEnter/OnTriggerEnter occurs, add that collider to the list. When OnCollisionExit/OnTriggerExit occurs, remove that collider from the list. You now have a list of all colliders that you are currently colliding with.

Comment
Add comment · Show 4 · 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 Eugenius · Jun 25, 2013 at 08:05 PM 0
Share

Not sure if it would work for multiple objects with colliders using the same tag.

avatar image iwaldrop · Jun 25, 2013 at 08:10 PM 1
Share

Of course it would.

avatar image Eugenius · Jun 25, 2013 at 08:14 PM 0
Share

OK then, thanks. I currently am unable to test this theory as I'm not at my build PC. I will come back here to mark your answer as correct if this works tomorrow ^_^.

Thanks for the quick replies!

avatar image Eugenius · Jun 27, 2013 at 05:38 AM 0
Share

Ok, it worked. I still have some minor glitches like making sure each collider is counted only once, but I'll try to figure that out on my own :)

avatar image
1

Answer by HunterKrech · Jun 25, 2013 at 07:51 PM

In unity script you would use somet$$anonymous$$ng like t$$anonymous$$s(Some what pseudo code, i don't know uniscript syntax)

 var enemyColliders = new Array();
 
 function OnCollisionEnter(collider : enemies)
         {
             enemyColliders.push(enemies.gameObject.collider);
         }

Now you can push, pop, and s$$anonymous$$ft colliders as necessary.

Comment
Add comment · Show 2 · 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 Eugenius · Jun 25, 2013 at 08:05 PM 0
Share

I do not need to push, pop or shift the colliders. I simply need to count them.

avatar image HunterKrech · Jun 26, 2013 at 05:04 PM 1
Share

Well after you push them aka add, then you can count them by enemyColliders.length

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Trigger Enter and Exit not working properly? 2 Answers

Destroy On Collision will not work with spawned AI 1 Answer

Generating A Custom Collider For Multiple Meshes - Script 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 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