- Home /
Accessing Singleton Instance Methods Directly
Is there a way to access a singleton method directly instead of going through it's instance?
// Instead of doing this,
GameManager.Instance.DealDamage(Target);
// Is there a way to do this?
GameManager.DealDamage(Target);
=====
GameManager.cs
using UnityEngine;
using System.Collections;
public class GameManager: MonoBehaviour {
// Singleton
private static GameManager instance;
public static GameManager Instance {
get {
return instance ?? (instance = GameObject.FindObjectOfType(typeof(GameManager)) as GameManager);
}
}
public void DealDamage (GameObject Target) {
// Deal damage to the passed target
}
}
Answer by Huacanacha · Oct 28, 2013 at 10:51 PM
If that's the behavior you want you should just use static methods, with static (class) variables if needed to store state.
public static void DealDamage (GameObject Target)
If you don't need to hook into Unity's standard functions like Awake, Start, Update etc, then don't inherit from MonoBehavior.
If you do want to hook into the Unity functions you can use a singleton instance to update your static variables, without needing to provide public access to the instance.
I'm not sure if I will need the $$anonymous$$onoBehaviour inheritance yet or not. I just left it on there for now. Would it be better to remove it until something comes up that I need it?
Great, I'm glad I could help. And thanks for accepting :)
I would remove the $$anonymous$$onoBehavior as it's unnecessary in the implementation you list below. I'll add more comments there.
Answer by Squabbler · Oct 29, 2013 at 03:16 PM
For those wondering, this is how I ended up doing it.
=======
GameManager.cs -> Attached to GlobalScripts prefab (empty GameObject)
using UnityEngine;
using System.Collections;
public static class GameManager {
// Event for triggering damage to an object
public delegate void OnDamageEvent(GameObject Go, int DamageAmount);
public static event OnDamageEvent OnDamage;
// Deals damage to the target
public static void DealDamage (GameObject Target, int DamageDealt) {
OnDamage(Target, DamageDealt);
}
}
=======
In the Enemy Controller
// Attack the target
protected override void Attack () {
anim.Play("Attack");
// Deal damage to target
GameManager.DealDamage(Target, Damage);
}
=======
In the possible target controllers
void Awake () {
GameManager.OnDamage += OnDamage;
}
// Damage event method
protected void OnDamage (GameObject Target, int DamageAmount) {
// Check if we have this game object
if (Target == gameObject) {
TakeDamage(DamageAmount);
}
}
In your current implementation you don't need to inherit from $$anonymous$$onoBehavior, and you also don't need the singleton methods as you never use the instance.
If you do want to leave in $$anonymous$$onoBehavior, the singleton technique I've seen used is to set the instance variable in the Awake method rather than via the get method of a Property, and attach the singleton class to a game object so Awake gets called. For organizational sanity I use an empty 'GameController' GameObject to attach all my game management scripts to (main GameController script, initialization etc).
I wasn't actually aware of C# Events as I'm fairly new to the language but they look useful... in fact I can simplify some of my own code now ;)
I tried to remove the $$anonymous$$onoBehaviour inheritance and the singleton code, but it wouldn't let me attach it to the GlobalScripts empty GameObject.
Is there another way I can access the Game$$anonymous$$anager script without attaching it to an empty GameObject?
You shouldn't need to attach the script to a GameObject for it to work with static functions. The class is still loaded by Unity so it should just work.
Oh. Derp. I forgot to set the class to 'static' when removing the inheritance and instance.
public static class Game$$anonymous$$anager
lol, it works now. I never knew Unity would still read a static class without it actually being attached to a GameObject. This is a game changer for sure.
I also edited the code to reflect my changes.
Your answer
Follow this Question
Related Questions
Singleton that can react to game events 2 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Singleton code and Unify down. 1 Answer