Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Count How Many Triggers Are Collided

Discussion in 'Scripting' started by QuantumCalzone, Jan 24, 2011.

  1. QuantumCalzone

    QuantumCalzone

    Joined:
    Jan 9, 2010
    Posts:
    262
    Can you count how many colliders are within a collide?



    For example in this images, each object would have a script that counts the number of colliders it is colliding with. How would you do that with more than one collision?
     
  2. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,249
    just use a global variable. Add one on triggerenter, and subtract one on trigger exit.
     
  3. QuantumCalzone

    QuantumCalzone

    Joined:
    Jan 9, 2010
    Posts:
    262
    But if the TriggerEnter is already enabled, is it still able to calculate another while it in the process?
     
  4. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,249
    I would of thought so if you attached the scripts to the object.

    Something like to the object. Then somewhere else you need a global var to keep count.
    Code (csharp):
    1.  
    2.  
    3. function OnTriggerEnter (col : Collider) {
    4.  
    5. //update global var in another script +1
    6.  
    7. }
    8.  
    9. function OnTriggerExit (col : Collider) {
    10.  
    11.  //update global var in another script -1
    12.  
    13. }
    14.  
    15. function Update () {
    16.  
    17. }
    18.