NullReferenceException when passing a gameObject as a parameter

I’m getting a NullReferenceException when I’m passing my game object as a parameter. Here is the relevant code. I just want to make sure I’m passing this correctly.

public class Astroid : PauseableObject, IDamage, IPoolable, ITakeDamage, IWarning
{

    private static NearOffScreenWarning nearOffScreenWarning;
    public bool warning = false;

    public void FixedUpdate()
    {
        Warn();
    }

    public void Warn()
    {
        if (nearOffScreenWarning.CheckIfInBounds(gameObject))
        {
            GameObject warningObject = ObjectPoolManager.ObjectPoolManagerSingleton.AsteroidWarnings.GetPooledObject();
            if (warningObject != null)
            {
                warningObject.SetActive(true);
                warningObject.transform.position = nearOffScreenWarning.left;
                warning = true;
                Debug.Log("Hello");
            }
        }
    }
}

public class NearOffScreenWarning : PauseableObject, IWarning
{
    int theThing = 0;

    public bool CheckIfInBounds(GameObject gameObject)
    {
        Vector2 pos = gameObject.transform.position;
        if (CheckObjectToRightOfScreen(ref pos) &&
            CheckObjectToLeftOfScreen(ref pos) &&
            CheckObjectToBottomOfScreen(ref pos) &&
            CheckObjectToTopOfScreen(ref pos))
        {
            Debug.Log("In Bounds");
            return true;
        }
        else 
            return false;
        }
    }
}

Hi,

You just need to define your nearOffsetWarning variable.

So your error come from the fact that nearOffsetWarning is undefined.

But actually, i don’t see your gameObject variable defined too. And i don’t know if your script is assigned to a gameObject, but if it isn’t, don’t forget to define your “gameObject” variable.

In order to do this, keep a look at Game.Find, GameObject.FindwithTag or just add a public attribute called gameObject to this class and drag your gameObject on this new field from the editor.

public GameObject gameObject;