can't reference an instance of an object (prefab)

Hi all
I’ve checked out other NullReferenceException questions, but I still don’t understand why the following doesn’t work:

public class Problem: Monobehaviour{

public GameObject spawnMarker;
public GameObject mySpawnMarker;

public void PreWaveEvent(){
   determineSpawnPoint();
   mySpawnMarker = Instantiate (spawnMarker, spawnPoint, Quaternion.identity) as GameObject;
   Debug.Log (mySpawnMarker.transform.position);
}

}

spawnMarker references a prefab I drag an drop into it in the editor.

What happens is the following:

an instance of spawnMarker is succesfully created, I can see it in game mode.

However, all attempts to reference it using “mySpawnMarker” fail and throw a Null Reference Exception (Debug.Log was just for debug purposes, I originally tried Destroy(mySpawnMarker) and others). I don’t understand why, because the same approach works for other objects in my game (though there I’m not instantiating from a prefab, but from another object that’s in the scene). Thanks for your help!

Hmm… I’ve had a few NRE’s with prefabs Instantiating it like: “~~~) as GameObject;”
Try this:

public class Problem: Monobehaviour{
 
public GameObject spawnMarker;
public GameObject mySpawnMarker;
 
public void PreWaveEvent(){
   determineSpawnPoint();
   mySpawnMarker = (GameObject) Instantiate(spawnMarker, spawnPoint, Quaternion.identity);
   Debug.Log (mySpawnMarker.transform.position);
}

Or this is how I do it. Have a prefab in your resources folder called ‘spawnMarker’, and disregard assigning it in the editor and just call it from script.

public class Problem: Monobehaviour{
 
public GameObject mySpawnMarker;
 
public void PreWaveEvent(){
   determineSpawnPoint();
   mySpawnMarker = (GameObject) Instantiate(Resources.Load("spawnMarker"), spawnPoint, Quaternion.identity);
   Debug.Log (mySpawnMarker.transform.position);
}
 
}