Prefab disappears upon loading scene

I have a gamecontroller script that loads a base script with an override script depending on the current game mode…

function Awake ()
	// Setup Mode Controller
	modeController = gameObject.AddComponent("ModeController_";

The game mode script that will be spawned (“ModeController_Prevention”) holds a reference to a prefab (GraveController) that needs to be instantiated…

alt text

class ModeController_Prevention extends ModeControllerBase
	var graveControllerPrefab : GameObject;
	var graveController : GraveController;	
	function Start ()
		// Create a GraveController for this round
		var gc = Instantiate( graveControllerPrefab );
		graveController = gc.GetComponent(GraveController);

When I run the scene direct, this all works perfectly and the ModeController script is attached and the GraveController prefab is instantiated. However when I’ve loaded my game’s Main Menu scene and then load this scene via that scene, the ModeController script gets attached correctly but the GraveController prefab is no longer there and results in the error, “The thing you want to instantiate is null.”

When you attach the ModeController script, it is attached without references to prefabs. Perhaps you could have another GameObject in the scene called, PrefabHolder, or some sort that contains a script, “PrefabHolder”, in which it has public GameObject called “graveControllerPrefab”. You assign the prefab to that variable via drag and drop to the inspector. Also, make a tag called “PrefabHolder” and assign the PrefabHolder GameObject with that tag. Thus, when the ModeController is attached, OnAwake it does:

prefabHolder = GameObject.FindGameObjectWithTag("PrefabHolder");

Then the ModeController can have a line OnStart():

graveController = prefabHolder.GetComponent<PrefabHolder>().graveControllerPrefab;

Finally when the ModeController is ready to spawn a GraveController, it is able to because the reference to a prefab has been assigned at Awake.

I hope this helps.

I realize you write in JavaScript, whereas my code is in C#. Hopefully it gives you the right idea though and that you can translate from it.