Hi there,
I’ve done this a few times now and it feels kinda hacky so I thought I’d just ask here and see what you think
I have a few scripts in my project that I need to access from other scripts all of the time. So with those scripts I’ve put something like this at the start
static public var Access : GUI_Master;
var GUI_Active : boolean;
function Awake(){
Access = this;
}
So from any other script I could use something like this -
if (GUI_Master.Access.GUI_Active == true)print ("Do Something!");
This is basically a trimmed down Singleton pattern.
It can lead to some traps if you over use it. And there’s one big issue, it means you can only have 1 of that class ever (hence the name Singleton). The design pattern should be used if you’re attempting to restrict to 1 instance, not for ease of access, the ease of access is just something that falls out of the pattern.
In the end though, no, it’s not necessarily hacky.
I actually wrote a base class for any singleton to inherit from that is a one spot stop for all the boiler plate code that a proper Singleton needs, and also so it deals well with Unity when loading and deleting GameObjects.
Hey thanks for clearing that up, I don’t mind too much if there’s only one of that class. I’m also not really overusing it so I reckon it probably will be okay. Just wanted to make sure I wasn’t doing something horribly wrong!