Setting singleton assets through inspector

So, I was trying to add a NGUI’s UIFont prefab into my singleton. Only to find out it’s not that easy.

I could add the prefab on Resources and initialize it on Start. That would work. But I didn’t want to do it like this because then I’ll have to “lock” the prefab name to a string.

Rather, I was trying to figure out a way to do it on the Inspector. Then I got stuck. It simply doesn’t work. Maybe it’s a bug. If I add my singleton into a scene, the default values for language will load just fine. But when it’s instantiated through script, nada.

Anyway, now here I am. Don’t even know what’d be the best approach to do it.

Is this a failure on the Singleton design?

I mean, we can and should set up most singleton variables on the source code itself. But for some assets it seems to make more sense to do it on the Inspector. I just can’t picture how.

You can use a prefab-based-singleton. It requires you to place the singleton class on a prefab inside a resources folder to ensure that the prefab is included in the build.

You have to change the “Singleton” class to not create an empty GameObject but instead use Resources.FindObjectsOfTypeAll to instantiate the prefab