How can I create a global reference to a function that instantiates?

Hi! Right now I’m creating a text-based RPG style game. So basically I want to use a utility function that can create a text object to be displayed. Considering this is text-based game, it’s probably going to be used in most scripts. However I’m not sure of the best way to access it.

  1. Static Method - As far as I know, this can’t be done when using the Instantiate function.

  2. Singleton - Although I’m pretty sure it’s viable to use, I can’t say I understand the full concepts behind these so I tend to not use them.

  3. ?

Once again, I’m not too sure on what I’m talking about, but as far as I know in general using global functions is considered bad practice. Can anyone shed some light on the topic please?

Static functions are the same as everything else : too much of one thing is a bad thing. If you use them properly, it should be fine. Same for singletons. In fact, the way to make singletons in c# is to use static fields. So it’s basically the same thing.

As for your problem, when you say text object, i assume you are talking about unity’s GameObject. Please understand that there is System.Object, Unity.Engine.Object and Unity.Engine.GameObject, so you might want to be less vague. If it is a GameObject, the you are probalby talking about UI. If this is the case, you can have a static function take in a prefab and text options (text to display, maybe some actions available to the player…) and will adapt the prefab to the given parameters.

Personally i’d go for a GameManager singleton using a monobehaviour that does the text displaying. Since you said it’s a text-based RPG, it should have a fair amount of text, and that includes the logic of how and where to display said text. It has enough complexity to be it’s own module. However, since it depends heavily on the access to the scene, you need it to inherit from monobehaviour. Just put it on the object that will be containing all the text and get it’s reference in a game manager.