static Prefab references?

Hi,

i just want to refer to my prefabs in a static way.

So i created a class called “Blueprints.cs” and noticed that this seems to need an object Reference.

I built up some singleton like stuff, but then noticed that all the objects are null i trying to create, even if i fill them up in the script.

public class Blueprints : MonoBehaviour 
{
    // To be preset
    public GameObject RightStab;
    public GameObject Ball;
    // Enemies
    public GameObject Wrench;

    public static GameObject WrenchStatic; // <<< won't work :-(
}

When i open this script in the inspector,
i can only drag prefabs to the “RightStab” and “Ball” and “Wrench” … the entry “WrenchStatic”
doesn’t appears.

In my ‘main’ script i did the following:

    public static Blueprints blueprints;
	void Start () 
    {
        GameObject go = new GameObject();
        go.AddComponent<Blueprints>();
	}

later in my code, i try to instantiate Gameobjects using this prefab.
But it doesn’t work because it says it was null.

        Instantiate(blueprints.RightStab); // Won't Work!

I’m perfectly used to instantiate Prefabs the usual way, but i want to keep all the objects that i may want to spawn in one single class for easier access.

Is there any way to instantiate them in a static way, or via filepath, or … maybe using this workaround i am trying to use here ?

Thanks in advance,
David

You cannot link prefabs to static variables directly. If you want to do something like this, you will need to put in an object with non-static fields in the scene, drag the prefabs in there and add static accessors to find them. Something like this:

public class Blueprints : MonoBehaviour
{
    public static Blueprints instance;

    public GameObject aPrefab;

    public static GameObject aStaticPrefab
    {
        get
        {
            return instance.aPrefab;
        }
    }

    void Awake()
    {
        instance = this;
    }
}

// you can now access the aPrefab by calling:
Blueprints.aStaticPrefab

(untested)

If you’re just looking for consolidation and easy access, then is there a reason that an enumerated array of your prefabs isn’t sufficient? Despise working with arrays in the inspector perhaps?