- Home /
Non-static Singleton implementation
Hey, I am trying to code a game state handler, with death events etc. and I would like to be able to "record" it throughout the game, so I can implement replay.
So how would I go about this? Since I can't implement with the usual singleton approach, as that would require static variables.
First idea was to use FindObjectsOfType in Start() but I can't find any examples of people doing this, so I'm kind of nervous to try it out. For safety/performance reasons.
Answer by whydoidoit · Jan 13, 2013 at 03:00 PM
If you know what you are doing with Singletons then go ahead and use static variables - anything else is too tortuous.
Recorder.js
static var instance : Recorder;
function Awake()
{
instance = this;
}
function OnDestroy()
{
if(instance==this)
instance = null;
}
So it shouldn't be a problem? $$anonymous$$y first thought was that since it's not changing, and defined on initialization, it should just be excluded from the serialization. And then recreated on reserialization.
But researching around have mainly given me the impression that static variables/properties will simply stop a class from being serialized.
Classes with statics will serialize fine - but the statics will not be serialized themselves as you say.
All that time spend on finding a solution to a problem that isn't there :S But thanks for the answer :)
Your answer
Follow this Question
Related Questions
Type UnityEngine.GameObject is not marked as Serializable. 1 Answer
SerializationException: End of Stream encountered before parsing was completed 1 Answer
Keeping information on custom Inspector window using List of List 1 Answer
Crash when using "Base" as member... is it a reserved word ? 1 Answer
Using a custom Start()/Awake() in non monobehaviour classes. 1 Answer