0

I'm trying to get all colliders of game objects with a layer mask "Zombie Layer" with a certain radius using Physics.OverlapSphere.

My problem is sometimes the code works and sometimes the I get this error below when I try to get the Zombie AI script component. I've ensured that all my zombie game objects have the ZombieAI script attached to them.

error

Here is the code:

public class SoundDetection : MonoBehaviour
{
    private float soundIntensity = 10f;
    public LayerMask zombieLayer;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Detect();
        }
    }

    private void Detect()
    {

        Collider[] zombies = Physics.OverlapSphere(transform.position, soundIntensity, zombieLayer);

        if (zombies.Length == 0)
        {
            return;
        }
        else
        { 
            for (int i = 0; i < zombies.Length; i++)
            {
                zombies[i].GetComponent<ZombieAI>().OnAware(); //error here
            }
        }

    }
}

The above code is in a different script placed in my fps controller. If I remove this script and attach it again, the code works until it stops randomly for some reason.

I tried looking at other solutions but couldn't figure anything out since I'm still new to Unity 3D.

Any ideas what I'm doing wrong?

Shane D'Silva
  • 405
  • 1
  • 4
  • 11
  • 1
    The answer in the [marked duplicate](https://stackoverflow.com/q/4660142/1260204) is pretty much a "how to troubleshoot a NRE", a guidebook if you will. Use the advice in that answer to figure out what is causing the NRE in your code – Igor Feb 14 '20 at 14:19
  • Like I said, I've seen that solution and it didn't solve my issue. My problem is I have the ZombieAI script component attached and GetComponent<>() is still returning null. – Shane D'Silva Feb 14 '20 at 14:22
  • Please also unmark my question as a duplicate as that question has nothing to do with Unity I believe. – Shane D'Silva Feb 14 '20 at 14:24
  • 2
    Is `zombies[i].GetComponent()` returning `null` or is `zombies[i]` returning `null`? – Igor Feb 14 '20 at 14:24
  • Then you have to figure out *why* is that item returning `null`. You can do that by stepping into the code at run time with your debugger attached. – Igor Feb 14 '20 at 14:25
  • See also the [documentation Collider.GetComponent](https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html), returning `null` is an expected possible result. You need to check for it in your code. You can do that with a check or make the subsequent call using the elvis operator ?. if you do not want to do anything if the result is null. – Igor Feb 14 '20 at 14:27
  • Finally, the duplicate question is applicable here as well. – Igor Feb 14 '20 at 14:28
  • zombies[i].GetComponent() returns null. On checking for Zombies[i], for some reason it's returning my bullet prefab – Shane D'Silva Feb 14 '20 at 14:28
  • Okay, so somehow, my bullet prefab's layer mask was set to Zombie Layer. That's why its crashing because that prefab does not have a zombie AI script. I don't know why I didn't do a log for Zombies[i] too. Thanks, @Igor :) – Shane D'Silva Feb 14 '20 at 14:31

0 Answers0