Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Overlap sphere not returning anything

Discussion in 'Scripting' started by Corva-Nocta, Oct 14, 2018.

  1. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    I'm trying to create small explosions in my game. When the explosion is created I want to get all colliders within a certain radius then do damage to them. I've tried OnTriggerEnter, OnTriggerStay, and using overlapsphere but nothing seems to be working. I even try adding debugs all over my code, but they don't even trigger. Not sure what is going on.
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Well, show us what you tried, well see why it didnt work
     
  3. AdamRamberg

    AdamRamberg

    Joined:
    Dec 8, 2016
    Posts:
    22
    Not sure if you are doing 2D or 3D, but assuming that you are doing 3D. I'm also assuming that you got 2 objects and when they collide an explosion should be triggered. I also assume from your questions that you are not getting any triggers at all, am I correct?

    A couple things that you should check:
    • One of the game objects' colliders should be set to "Is Trigger"
    • One of game objects should have a rigidbody attached (see docs)
    • Check that you use the correct spelling for the OnTriggerEnter function
     
  4. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Here's some code to show what I have tried:

    Code (csharp):
    1.  
    2. void Explode(Vector3 center, float radius)
    3.     {
    4.         Collider[] hitColliders = Physics.OverlapSphere(center, radius);
    5.         foreach (Collider hitObject in hitColliders)
    6.         {
    7.             if (hitObject.tag == "Enemy")
    8.             {
    9.                 EnemyHealth enemyHealth = hitObject.GetComponent<EnemyHealth>();
    10.  
    11.                 if (enemyHealth != null)
    12.                 {
    13.                     enemyHealth.TakeDamage(alienDamage);
    14.                     Destroy(gameObject);
    15.                 }
    16.             }
    17.             else if (hitObject.tag == "HiddenPart")
    18.             {
    19.                 ObjectHealth objectHealth = hitObject.GetComponent<ObjectHealth>();
    20.  
    21.                 if (objectHealth != null)
    22.                 {
    23.                     objectHealth.TakeDamage(objectDamage);
    24.                     Destroy(gameObject);
    25.                 }
    26.             }
    27.         }
    28.     }
    This was the first one I was trying with the overlap sphere. Doesn't do anything at all.

    Code (csharp):
    1. OnTriggerEnter(Collider hitObject)
    2. {
    3.         Collider[] hitColliders = Physics.OverlapSphere(center, radius);
    4.         foreach (Collider hitObject in hitColliders)
    5.         {
    6.             if (hitObject.tag == "Enemy")
    7.             {
    8.                 EnemyHealth enemyHealth = hitObject.GetComponent<EnemyHealth>();
    9.  
    10.                 if (enemyHealth != null)
    11.                 {
    12.                     enemyHealth.TakeDamage(alienDamage);
    13.                     Destroy(gameObject);
    14.                 }
    15.             }
    16.             else if (hitObject.tag == "HiddenPart")
    17.             {
    18.                 ObjectHealth objectHealth = hitObject.GetComponent<ObjectHealth>();
    19.  
    20.                 if (objectHealth != null)
    21.                 {
    22.                     objectHealth.TakeDamage(objectDamage);
    23.                     Destroy(gameObject);
    24.                 }
    25.             }
    26.         }
    27.     }
    same result. Nothing happens. I event tried this one but with OnColliderEnter, OnColliderStay, OnTriggerStay, and all sorts of stuff like that. I even put Debug.Logs as the very first line in each of these and it never fires. Even more odd, I do not have a line that destroys these explosions once they are created, so theoretically when an enemy enters them they should trigger. Haven't gotten anything to trigger at all yet.

    any thoughts?
     
  5. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Ah I have all of those checked off except for the rigidbody. I bet that is the issue
     
    AdamRamberg likes this.
  6. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    It seems pretty right besides the call to Destroy(gameObject),
    what its doing is destroying the object the script is on, dont know what you exoect it to do
     
  7. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Hm, I tried adding a rigidbody, still nothing is happening as far as I can tell. On top of that, it makes the enemies with rigidbodies move in really strange ways, also super fast. I don't this a rigidbody is what I want to use for this. I have rigidbodies on things like my bullets and those work just fine with OnTriggerEnter but I suspect that is because something is actually entering the collider.

    maybe I should just find all the colliders that are within a radius using code?
     
  8. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    That's the weird part though, it all seems right but it doesn't work. The Destroy(gameObjects) don't even get called.

    I have colliders on the explosions and on the enemies I want to hit, the explosions are set to be triggers. Its just odd that the OnTriggerEnter never gets called, even when something enters the collider
     
  9. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    If you put debug.logs in the loop does any get called?

    Are you sure the function is being called?

    Where and how are you calling it?
     
  10. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    oh my god....... I am not fit to be called a game designer. I found the problem. I never attached the explosion script to the explosion object............ no wonder it wasn't working!!!!! Thanks for all the help guys!
     
  11. AdamRamberg

    AdamRamberg

    Joined:
    Dec 8, 2016
    Posts:
    22
    Haha, happens to the best of us! Great that you solved it :)
     
    bison-- likes this.
  12. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    True that, hahaha
     
    bison-- likes this.