Is there an easy way of telling how many GameObjects are contained within a mesh?`

I am currently trying to keep track using “OnTriggerStay”. Basically keep a log of GameObjects that started causing that to fire but it is not working :frowning:

Is there some easy API that I am missing or is it going to be a brutal OnTriggerStay bookkeeping exercise?

When OnTriggerEnter is called, +1
When OnTriggerExit is called, -1

@Joe-Censored you are a great great man. I thank you, the world thanks you.

@Joe-Censored sorry for coming back to the well again but…

It seems like when an object is destroyed the “OnCollisionExit()” is not being triggered, is that expected or am I doing something wonky?

When Objects are instantiated “OnCollisionEnter()” is triggered

? But you’re destroying it, so you can count it, don’t you?

Unfortunately the way these GameObjects are appearing is that if a sensor (a real life hardware sensor) sees a person it creates a person GameObject. If it does NOT see a person, it deletes the person GameObject. So it is non-deterministic as it is running in another thread.

I didn’t know you were destroying them. I don’t know the answers to your two questions off the top of my head. I’d have to test them (which I’m too lazy to do, sorry :stuck_out_tongue: ).

I’d consider two approaches.

  1. Use SphereCast or BoxCast to find all objects within the collider’s area. If the collider you’re using is already a regular shape, such as a cube or sphere, then this should be pretty easy. You can then find how many objects are within this space whenever needed.
  2. When you instantiate one of these objects, you add it to a list. When you destroy one of these objects, you remove it from the same list. Then whenever you need to you can just iterate through the object list and check for whatever criteria you need to, such as distance to something, etc.

https://docs.unity3d.com/ScriptReference/Physics.SphereCast.html

I highly doubt that. At least last time I checked Unity API wasn’t thread-safe and thus was only available from the main thread. So if you destroy game objects, you have to be on the main thread. Or I’m misunderstanding something in your setup.

Sorry I mean

This is great thank yoU!

1 Like

Yea I am breaking some rules lol, but it works! (except for this)

1 Like