- Home /
Physics.OverlapSphere not returning anything
Hello. I'm trying to make a script that deactivates a couple of bullets that are near the player. I saw that Physics.OverlapSphere should put all GameObjects in a radius in an array, then I could check each element of that array and see if it has a tag. I made/stole this code from the documentation:
public class BlankEffectScript : MonoBehaviour
{
public Transform transformThis;
void Start()
{
Invoke("Blank1", 0.1f);
}
void Blank1()
{
Debug.Log("function executed");
Collider[] hitColliders = Physics.OverlapSphere(transformThis.position, 5);
foreach (var hitCollider in hitColliders)
{
Debug.Log(hitCollider);
if(hitCollider.tag == "EnemyBullet")
{
hitCollider.gameObject.SetActive(false);
}
}
}
void OnDrawGizmos()
{
Gizmos.DrawWireSphere(transformThis.position, 5);
}
}
However, when a GameObject runs it, Physics.OverlapSphere
is not returning anything. I'd know if it returned anything, as on the foreach()
loop it'd debug every component on the hitColliders
array. This is what happens when the GameObject with BlankEffectScript
is Instantiated:
As you can see on the console only "function executed
" is debugged. The GameObject with the BlankEffectScript
script has a circle collider ( trigger ) and a Dynamic Rigidbody2D with 0 gravity and no rotation on the X, Y and Z axis. The GameObjects that should be in the hitColliders
array ( bullets ) have a couple of scripts and a circle collider ( trigger ). I also tried adding a Dynamic Rigidbody2D with no gravity to them, but it did solve my problem . Is there something wrong with the script? Any help would be appreciated.
Your answer
Follow this Question
Related Questions
Cannot convert 'UnityEngine.Collider[]' to 'UnityEngine.gameObject[]' using OverlapSphere 3 Answers
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
2D version of Physics.OverlapSphere? 2 Answers
Physics.OverlapSphere, if statement, gameObject.tag 0 Answers
Keep instantiated GameObject's position while parenting it to another GameObject 3 Answers