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 abcd_lsg · Jun 17, 2020 at 09:47 AM · collidercolliderstriggersstatic

number of colliders inside a trigger.

Is there a variable that keeps track of the number of colliders inside a trigger at any given point in time? I've tried using a variable that increments up and down on the OnCollisionEnter() and OnCollisionExit() functions but OnCollisionExit() doesn't seem to tick it down if the collider object is destroyed while its within my trigger leaving the value a unit above what it should be until i restart. I've tried looking into the Collision class a little bit but it only seems to be a method of passing information onto the OnCollisionEnter() and OnCollisionExit(), without any static instances keeping track of contacts at all points in time.

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
1
Best Answer

Answer by xxmariofer · Jun 17, 2020 at 01:03 PM

there is no variable keeping track for the number of colliders iside the trigger, but there is another event called OnCollisionStay() that will get fired every physics iteration so you can count them manually if needed

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 abcd_lsg · Jun 24, 2020 at 01:57 AM 0
Share

This seems like it might be what i'm looking for, but i'm having trouble thinking of a way that would keep it updated in real time. If i'm understanding it correctly, i would need to keep track of how many onTriggerStay()'s get called within a given frame, and that would essentially be the number i'm looking for.

I've tried putting collisionCount++; in the onTriggerStay and then collisionCount = 0; in my update function (after where its used) but these don't seem to align themselves really well in realtime and results in CollisionCount flickering between 0 and 1 when it should just be staying at 1.

Im going to try to make a c# list and use ontriggerstay to keep it updated with the gameObject ID's of everything that's in it, that way the size of the list would be exactly the number i'm looking for, but i'm not that familiar with the subsurface code behind c# lists so i don't expect this to go very well.

Would there be a simpler way to achieve the same effect?

avatar image abcd_lsg · Jul 17, 2020 at 06:40 AM 0
Share

Forgot to update: I figured out i can just set a float as a buffer variable and set it to a specific number for every onTriggerStay() and subtract Time.fixedDeltaTime from it every fixed update, so that i have a variable that tells me how much time it was been since the last object left the trigger, this isn't exactly what i was looking for but it works for what i need, ty.

avatar image
0

Answer by FrameXdropProvrex · Jun 17, 2020 at 05:39 PM

For me it works perfectly to increment and decrement a value in triggerenter and triggerexit, even when i'm destroying the gameobjects while they're in the trigger (i use this for an AddToInventory script). this is what works for me:

 public virtual void OnTriggerEnter(Collider other)
 {
     StorableItem item = other.GetComponent<StorableItem>();
     if (item)
     {
         collisionCount++;
         currentItem = item;
     }
 }

 public virtual void OnTriggerStay(Collider other)
 {
     if (currentItem != null)
     {
         if (Grab.stateUp || Pinch.stateUp)
         {
             InventoryAddBehavior(currentItem, currentItem.gameObject);
         }
     }
 }

 public virtual void OnTriggerExit(Collider other)
 {
     StorableItem item = other.GetComponent<StorableItem>();
     if (item)
     {
         collisionCount--;
     }
 }
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 abcd_lsg · Jun 24, 2020 at 02:12 AM 0
Share

So right now i just have OnTriggerEnter(collision){collisionCount++;} and OnTriggerExit(collision){collisionCount--;} and this mostly works fine, but even without the gameObjects that destroy themselves it still sometimes gets an extra 1 added to it. I'm not sure exactly what causes this because its a much rarer glitch than the one in the post, so i haven't been able to replicate it on command, but i need a solution that will solve both, and the only fix i can think of that will also solve the rarer version of this glitch would be to check it (groundedCount) every frame.

The guy above brought up using onTriggerStay, which sounds like it might be what i need but i'm having trouble thinking of a way to count the objects in my trigger with this function, have any ideas by chance?

Either way thanks for your time.

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

174 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 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 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 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

Triggers? How do I use them to finish a game? 1 Answer

Problems with the Trigger Collider (randomly fictional) 1 Answer

Does OnTriggerStay ignore layers? 1 Answer

Destroying objects within collision 1 Answer

changing gravity OnTriggerEnter 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