- Home /
Easily reference class instance
Currently I'm storing references of my object instances via static variables which is causing problems when trying to reset my game.
public class CameraFade : MonoBehaviour {
public static CameraFade current;
void Awake () {
current = this;
}
}
The nice things about this is I can then call this instance via Camera.current which is fast and easy in my code. Is there an alternative way to easily call an instance as so below without having to crawl through the hierarchy (which creates a mess in Awake methods)?
camera.current.DoSomething();
Comment
Best Answer
Answer by DoTA_KAMIKADzE · May 03, 2015 at 04:42 AM
Well you can make custom class that will hold your static variables and Reset all of them or part of them when you need:
internal static class EasyAccess
{
public static CameraFade current;
public static SomethingElse currentElse;//etc.
static EasyAccess()
{
//set initialization values here, or call Reset() here
}
internal static void Reset() //call this when you need reset
{
//set your values to default here
}
//or you can make even reset with your loading level and call it instead of LoadLevel, e.g.:
internal static void Reset(string levelName)
{
Application.LoadLevel(levelName);
}
//also you can make something like this:
internal static void ResetAllCamera() //call this when you need reset cameras only
{
//set your camera values to default here
}
}
Well just be creative with that one^ and all your resets can shrink down to 1 line of code. I can't think of any "lazier" way.