Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
3
Question by RickyX · Jan 10, 2016 at 01:54 PM · collisionmultipledetection

How many colliders i'm colliding with

Could someone give me an idea of how to check how many colliders with the specific tag my object is touching currently, without using CollsionEnter. I tried making a value, and adding +1 each time CollisionEnter is called, and doing -1 when CollisionExit is called. And it works but when i do some other stuff, the value i made just stays example 4 eventhough it's not touching any colliders currently. So is there another method ?

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

4 Replies

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

Answer by taylank · Jan 10, 2016 at 06:17 PM

Unity collisions can sometimes be finicky and difficult to track with CollisionEnter/Exit messages.

One thing you can do is to track each object of interest to see whether they are involved in a CollisionEnter or CollisionStay event that frame. So if GameObject A has not entered the collider this frame, and is not staying within the collider this frame, therefore it must be not currently colliding. This way you don't need to rely on CollisionExit event which may sometimes not fire.

Taking the event execution order into account:

 //make a list to track collided objects
 List<Collider> collidedObjects = new List<Collider>();
 
 void FixedUpdate() {
      collidedObjects.Clear(); //clear the list of all tracked objects.
 }
 
 
 // if there is collision with an object in either Enter or Stay, add them to the list 
 // (you can check if it already exists in the list to avoid double entries, 
 // just in case, as well as the tag).
     void OnCollisionEnter(Collision col) 
    {
          if (!collidedObjects.Contains(col.collider) && col.collider.tag == desiredTag) 
         {
               collidedObjects.Add(col.collider); 
          }
     }
 
 void OnCollisionStay(Collision col) {
      OnCollisionEnter(col); //same as enter
 }
 
 
 void Update() {
 var numberOfColliders = collidedObjects.Count; // this should give you the number you need
 collidedObjects.Clear(); // You can also clear the list here
 }

If the above method does not work, you may also want to try the answer here.

Comment
Add comment · Show 5 · 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 lassade · Jan 11, 2016 at 03:08 AM 0
Share

$$anonymous$$abe a hash will improve the peformance

avatar image RickyX · Jan 11, 2016 at 08:54 PM 0
Share

I'm really sorry but i solved my problem in a diffrent way already, after i posted the question i just worked on it and figured out a solution. But this might be helpful if anyone else runs into the same thing. Thanks anyways !

avatar image cojoc07 · Mar 13, 2017 at 09:56 AM 0
Share

Why use a list? Just use an int that you increment or decrement

avatar image Vahradrim cojoc07 · Apr 17, 2017 at 01:25 PM 0
Share

Of course not, a list is necessary to check the collider presence, for those already there. Leaving it as an integer will increment the number every frame since it's called in collisionStay.

As for collisions, call FixedUpdate, not Update, physics is recalculated at FixedUpdate.

avatar image prakharexjailia · Mar 13, 2017 at 10:41 AM 0
Share

Yes we can but idea will remain same.

avatar image
2

Answer by TheCookieMan_ · Aug 26, 2020 at 03:53 PM

 public class crafting : MonoBehaviour
 {
     public float objects;
     
     void OnTriggerEnter(Collider collision){
       if(collision.gameObject.tag == "your tag"){
         iron = iron + 1;
       }
     }
     void OnTriggerExit(Collider collision){
       if(collision.gameObject.tag == "your tag"){
            iron = iron - 1;
       }
     }
 
 
     void Update()
     {
         Debug.Log(objects);
     }
 }
 

This is WAY easier.

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 TheCookieMan_ · Aug 26, 2020 at 03:57 PM 0
Share

and yes I know I'm late

avatar image Razvan_Savin · Jan 04 at 05:32 PM 0
Share

Thank you!!! you are a hero :D

avatar image
0

Answer by prakharexjailia · Mar 13, 2017 at 09:49 AM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class hover : MonoBehaviour {
 
     List <Collider> collidedObj = new List<Collider> ();
         
     /*In single cycle script execution it counts number of Object entered and exited that is stored in collider list.
     I made some hover spheres which collids with triggerd box collider
     this works but I have no idea about it's accuracy*/
     void OnTriggerEnter(Collider obj)
     {
         collidedObj.Add (obj.GetComponent<Collider>());
     }
 
         void OnTriggerExit(Collider obj)
     {
         collidedObj.Remove (obj.GetComponent<Collider>());
     }
 
     void Update()
     {
         int objCollidedCount = collidedObj.Count;
         print (objCollidedCount);
     }
 }
 

//If anyone has better script please post here..

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 NatCou · Feb 02, 2018 at 11:18 AM 0
Share

$$anonymous$$aybe check to see if the collided object already exsists on the list before adding again?

avatar image
0

Answer by Razvan_Savin · Jan 04 at 05:34 PM

Count 3 different objects with tags. Have fun :D

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Counter : MonoBehaviour
 {
     // Text buttons
     public Text CounterBlueText;
     public Text CounterRedText;
     public Text CounterYellowText;
 
     private int blueCount = 0;
     private int redCount = 0;
     private int yellowCount = 0;
 
     private int countRed;
     private int countBlue;
     private int countYellow;
 
 
     private void Start()
     {
         blueCount = 0;
         redCount = 0;
         yellowCount = 0;
 
     }
     void OnTriggerEnter(Collider collision)
     {
         if (collision.gameObject.tag == "Blue")
         {
             blueCount += 1;
             CounterBlueText.text = "Blue : " + blueCount;
         }
 
         if (collision.gameObject.tag == "Red")
         {
             redCount += 1;
             CounterRedText.text = "Red : " + redCount;
         }
 
         if (collision.gameObject.tag == "Yellow")
         {
             yellowCount += 1;
             CounterYellowText.text = "Yellow : " + yellowCount;
         }
 
     }
 
 }
 
 ![alt text][1]


[1]: /storage/temp/190760-count.png


count.png (319.9 kB)
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

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

Related Questions

collision wont work 1 Answer

Collision isn't detected between two obects 1 Answer

Collision not working 3 Answers

Detecting Trigger Collision for Mecanim Animator 0 Answers

Collision detection not working properly with 2D sprites 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