Instantiated GameObject variable returning null

Hi there, I have this bit of code:

Debug.Log (spawnedObj);
   
        if(spawnedObj == null)
        {
            CreatePosition();
            spawnedObj = Instantiate(prefab,spawnPos,Quaternion.identity) as GameObject;

        }

The object is spawning, however when I debug spawnedObj, it’s returning null and thus constantly instantiating this object.

What have I done wrong?

Thank ye

You are logging before the instantiation happens. If you move it below the if statement does it still log as null?