Search Unity

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

Discussion in 'Scripting' started by adammpolak, Oct 20, 2021.

  1. adammpolak

    adammpolak

    Joined:
    Sep 9, 2018
    Posts:
    450
    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 :(

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

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    When OnTriggerEnter is called, +1
    When OnTriggerExit is called, -1
     
  3. adammpolak

    adammpolak

    Joined:
    Sep 9, 2018
    Posts:
    450
    @Joe-Censored you are a great great man. I thank you, the world thanks you.
     
  4. adammpolak

    adammpolak

    Joined:
    Sep 9, 2018
    Posts:
    450
    @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
     
  5. ? But you're destroying it, so you can count it, don't you?
     
  6. adammpolak

    adammpolak

    Joined:
    Sep 9, 2018
    Posts:
    450
    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.
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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 :p ).

    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
     
  8. 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.
     
  9. adammpolak

    adammpolak

    Joined:
    Sep 9, 2018
    Posts:
    450
    Sorry I mean
    This is great thank yoU!
     
    Joe-Censored likes this.
  10. adammpolak

    adammpolak

    Joined:
    Sep 9, 2018
    Posts:
    450
    Yea I am breaking some rules lol, but it works! (except for this)
     
    Joe-Censored likes this.