I already asked that one some time back, but because I mingled with code, and it changed couple times, and the answer is for the old answers and everything essentially went off the hook. I really feel the need to start that one over, I’m sorry for inconvenience. The original talk can be found here , although I’ve forsaken it. Do not use it as reference of context. Context and nuance of question have been altered.
What I attempt at creating in this tiny demo is a system, in which user can select an in-game character (one of many), and then have things like HP bar automatically adjusted in a GUI screen (when character is damaged). For this I choose to create a List<>
with users, based off a class, and a single variable based off the same class. Every time user clicks on a player, it will assign that particular human to that single variable. Every time this human loses health (due to nature of classes), it will update it both in variable containing all users, as well as this single variable. I want to watch out every time the single variable is modified, so I can trigger certain functions that will allow UI adjustment, if health of a selected human changes, UI will receive signal and resize the bar and change color accordingly. Now here’s the code (simple tech demo):
PlayerMaster, which will assign and invoke GUI functions
public class PLAYERMASTER : SerializedMonoBehaviour {
// I 1000000% set this in Inspector
public PLAYERINDICATOR indicator;
public class PlayerClass {
public string name;
public float health;
}
private PlayerClass _taggedPlayer;
public PlayerClass taggedPlayer {
get { return _taggedPlayer; }
set {
print("taggedPlayer+set triggered. HP: " + value.health);
_taggedPlayer = value;
indicator.UpdateHealth(value.health);
}
}
public List<PlayerClass> listOfAllPlayer;
private void Awake() {
listOfAllPlayer.Add(new PlayerClass { name = "Robbie", health = 325.0f });
listOfAllPlayer.Add(new PlayerClass { name = "Dirk", health = 500.0f });
taggedPlayer = listOfAllPlayer[1];
}
[Button("Remove Health", ButtonSizes.Medium)]
void _x_() {
taggedPlayer.health -= 11.0f;
}
}
PlayerIndicator:
public class PLAYERINDICATOR : MonoBehaviour {
public float health;
public void UpdateHealth(float hp) {
print("UpdateHealth() triggered: " + hp);
this.health = hp;
}
}
In aforementioned (old) thread, following has been mentioned:
So with that all out of the way. How would I make it work? How do I trigger it when the object it references gets altered, or otherwise, how would I get it to work in more or less same schematics.
Note: Due to game/application context in which it will be used, I cannot have PlayerMaster’s functions (not accessors) directly invoke PlayerIndicator’s functions (not accessors). I really need that particular selected player put in that public PlayerClass taggedPlayer
, no way around that. (Unless there’s a logical solution that I don’t know of, which does work and does what I need).
I hope that I’ve written big enough of an essay that one wouldn’t be able to confuse of what my intent is.
PS: No errors. The only debug logs I get (once each) is:
taggedPlayer+set triggered. HP: 500
UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
and:
UpdateHealth() triggered: 500
UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])