- Home /
Creating a global function, callable from anywhere
I'm making a first person game, and want to create pickup objects for the player to collect.
Ideally, I would be able to call something like:
PickupManager.CreatePickup(PickupType.ROCK, transform.position);
Also, I'd like to specify the prefabs to create by dragging prefabs onto the manager script variables via the inspector.
It seems if I create a static class, then I can't specify prefab types via the inspector (because the vars are static and not exposed).
If I create a singleton with a GetInstance() function, the instance doesn't seem to inherit the vars I assign via the inspector.
Should I be using a static class? A singleton? Do I need to attach a script to a gameobject?
Answer by gfr · Oct 24, 2011 at 06:21 PM
If you want to drag'n'drop prefabs on it in the Inspector you have to attach it to a GameObject.
You could handle this by simply only having one GameObject with the script on it in the scene, with the script containing something like this:
private static var instance : PickupManager;
function Awake() {
if (instance) {
Debug.LogError("More than one instance.");
}
instance = this;
}
static function GetInstance() : PickupManager {
return instance;
}
... which could then get properties for prefabs etc. and would be used like this:
PickupManager.GetInstance().CreatePickup(PickupType.ROCK, transform.position);
Your answer
Follow this Question
Related Questions
Why should I use a game object for non-physical things? 2 Answers
Bug caused by Singleton Pattern 1 Answer
Singleton GameState broken delegate? 2 Answers
How to make an abstract pseudo-singleton class 1 Answer
GameManager and scene design issue. 1 Answer