' The prefab you want to instantiate is null'

Hi, i’m simply trying to create a class with parameters, such as a gameobject name etc…
and bring them over to my ‘zone’ script. but I get the empty prefab error.

I understand that it’s not running the awake function in my ‘buildSphere’ first, so it’s not being set. How would I go about running that? I’ve tried instancing the buildSphere script > making the gameobject non static, but then I come into some monobehaviour problems. Stuck with this.

Any help would be greatly appreciated!


public static GameObject zoneTowerObj;
	public static int strength = 15;
	public static string towerName = "Awesome build sphere";
	public static int minHealth = 0;
	public static int currHealth = 90;
	public static int maxHealth = 100;

	void Awake(){

		if (!zoneTowerObj) {

						zoneTowerObj = Resources.Load ("buildSphere") as GameObject;

				} else { 


Zone Script:

	if (!zoneTower) {

				if (GUI.Button (new Rect (10, 300, 100, 20), "Add Tower")) {

			    dir = transform.position;

				zoneTower = buildSphere.zoneTowerObj;
				zoneTowerName = buildSphere.towerName;
				zoneTowerStength = buildSphere.strength;
				zoneTowerMinHealth = buildSphere.minHealth; 
				zoneTowerMaxHealth = buildSphere.maxHealth;
				zoneTowerCurrHealth = buildSphere.currHealth;

				Instantiate (zoneTower, dir, Quaternion.identity);
				zoneTower.name = zoneTowerName;



Instead of setting it in Awake(), which can cause a race condition if you try to access it before it is set, use the following code which will lazily fetch it as required (but only once):

private static GameObject zoneTowerObj = null;
public static GameObject GetZoneTowerObj() {
    if (zoneTowerObj == null) {
        zoneTowerObj = Resources.Load("buildSphere") as GameObject;
        if (zoneTowerObj == null) {
            throw new Exception("Cannot find resource buildSphere");
    return zoneTowerObj;

And whenever you want to use the obj, use buildSphere.GetZoneTowerObj();

Now, remember that resources must be under a folder named “Resources” in order to use Resources.Load. So you must have a prefab called “buildSphere” under a directory named “Resources” in your project structure.