Object reference unassigning itself at runtime

I’m trying to reference the grid object which contains a script that has my inventorygrid class. I’m referencing from another script attached to the ui camera.
8996929--1239235--upload_2023-5-7_6-2-31.png
Its assigned like this:
8996929--1239238--upload_2023-5-7_6-2-53.png

I’m just trying to reference it like this:

public class InventoryController : MonoBehaviour
{
    [SerializeField] private InventoryGrid selectedInventoryGrid;

    private void Update()
    {
        if (selectedInventoryGrid = null)
        {
            Debug.Log("InventoryGrid IS null.");
            return;
        }


        selectedInventoryGrid.GetTileGridPosition(Input.mousePosition);
    }
}

However, when trying to run the referenced class and method, I get “Object reference not set to an instance of an object.”. I’ve noticed that the “Selected Inventory Grid” that I’ve assigned just unassigns itself when I run the game.

In C# = is used to assign things and == is used to compare.

Here you are assigning it to null.

1 Like

selectedInventoryGrid = null

should be

selectedInventoryGrid == null

1 Like