- Home /
Static List< GameObject> in Singleton
I am using this code...
using UnityEngine;
using System.Collections; using System.Collections.Generic;
public class TextManager : MonoBehaviour { private static TextManager instance;
public static TextManager Instance
{
get
{
if (instance == null)
{
instance = new GameObject ("TextManager").AddComponent<TextManager> ();
}
return instance;
}
}
void Start()
{
Letters = new List<GameObject>();
}
public static List<GameObject> Letters;
public static GameObject MakeWord(string Text)
{
char[] letrs = Text.ToCharArray();
foreach(char letr in letrs)
{
foreach(GameObject letter in Letters)
{
if(letter.name == letr.ToString())
Debug.Log(letr);
}
}
return null;
}
void OnDisable()
{
instance = null;
}
}
I drop the script on a game object so I can add meshes I am using as 3d text to it.
But, the list does not show up on the object for drag'n'drop. I have used singletons before in c# but not in unity, am I doing it wrong? Or can I not use static lists of game objects? or? There are no errors, the list simply does not show up in the editor.
PS. I have edited this thing three times and cant get the code to show up correctly.
It's a huge pain in the butt to get the code to format sometimes...
You might have to copy/paste it elsewhere, then paste it back.
Answer by WillTAtl · Dec 02, 2011 at 07:16 PM
static members don't show up in the inspector, but since the class is a singleton, the list doesn't need to be static, there'll only be one copy accessed through the single instance.
Note even this this probably isn't going to work like you expect; you can only inspect isntances of an object on an object in the scene, and this script doesn't exist on an object until some other script triggers it's creation through the Instance getter. This means you'll only be able to inspect and modify the list at run-time.
Myself, I usually don't bother with this style of singleton pattern, I just place a single instance on a special object in the editor with an exposed public static instance member that is set in Awake, and throw an error if instance is already set when awake is called, to detect cases where I screwed up and put more than one in the scene.
basically, this:
public class MySingleton : MonoBehaviour { public static MySingleton instance=null;
void Awake()
{
if (instance!=null)
Debug.LogError("You created multiple instances of MySingleton!");
instance=this;
}
};
The list has to be static or it throws an error.
So your suggestion is..do as I am doing except ins$$anonymous$$d of making the class singleton, simply throw an error on awake if there is an instance of it out there?
The error I assume you're referring to is in $$anonymous$$akeWord where you access Letters directly, you just change that to instance.Letters, or change the function to a non-static function.
There's two basic approaches to singletons: $$anonymous$$ake everything static, or prevent making more than one instance and provide a way for anyone to get it. In unity, if you go the "everything static" approach, you won't be able to do anything to the object in the inspector at all, because static members never appear there. So you need an instance, if you want to inspect it, and if you want to be able to modify it in the inspector while editing, ins$$anonymous$$d of just while playing, you need the members to be non-static and you need the single instance to be placed in the scene directly ins$$anonymous$$d of creating and placing it while the game is running.
You can do it however you like, I'm sure there are others who will object to this informal approach as well, but the simplest solution with the least lines of code is the usually the best solution in my book.
Really all I want is to be able to create text from the files. I can't seem to access them from code without dropping them in a gameobject, I assume it has to do something with asset strea$$anonymous$$g which is pro only. I plan to get pro but only once the program is closer to completion as the investor (my husband, hah) will not allow the purchase of a pro licence till I have the prototype done :P
tangental to the q, but making prefabs won't break the blender connection. Prefabs won't copy the mesh imported from blender, just reference it, so they will still update automatically :)