I often find myself using private static variables for sharing information between several instances of the same class (I'm against the general over-use of the whole Manager-idiom for reasons that I won't get into here). For instance, in a game I'm currently working on where the player can select several entities in the scene, I have this (slimmed down for relevancy);
using System.Collections.Generic;
using UnityEngine;
public class Selectable : MonoBehaviour
{
private static List<Selectable> _currentlySelected = new List<Selectable>();
public static IEnumerable<Selectable> currentlySelected {
get { return _currentlySelected; }
}
public void Select() {
_currentlySelected.Add(this);
}
public void Deselect() {
_currentlySelected.Remove(this);
}
public static void DeselectAll() {
_currentlySelected.Clear();
}
}
In this particular case, all is well since the behaviour itself adds the information to the static variable. But what happens when I want a GUI for selected items, say a health bar? I could solve this by making a new GameObject to the scene with its own GUI-script, but using OnGUI would be much more elegant. Here is my issue however. I obviously want a texture or two for my healthbar, and I obviously want all my `Selectable` to share the same texture, so how do I assign it?
What would be best in my opinion was if I could make another static variable typed as Texture, and assign it once from the editor. That obviously won't work as the Inspector in the editor does not show static variables. Another solution would be to have a public Texture defined on a per-object basis and in my `Awake()`-method have something like this:
public void Awake() {
if (_staticTexture == null && _nonStaticTexture != null) {
_staticTexture = _nonStaticTexture;
}
}
That way I would only have to assign the variable once and all objects would share it, but it's neither elegant nor effective having to remember what object had the actual reference.
How would you go about solving an issue like this? I'm open for all kinds of alternatives so even if this gets a couple of answers, do not feel your opinion is irrelevant.