I want to access a variable globally. Say, in an rts game, all the buildings can spawn units, but they share one “Cash” integer. How can this be done? The buildings would need to read(Do I have enough money) and write(I’m using this money)to this variable.
Also, since this is going to be a multiplayer rts game, the variable should not be shared over the network.(each client has there own value)
Any and all help appreciated,
MTracer.
If each player has their own script/class, then you attach a Cash property to that script. All the buildings that you create for a player should be given a reference to the player they’re assigned to. With that reference, you can access the cash:
MyBuilding.AttachedPlayer.Cash
EDIT: nvm it’s working! W00T!
It keeps saying that “Cash” is not a member of GameObject.
gameObject.Find("resourceManager(Clone)").Money -= 50;
Thanks,
MTracer.
That’s because Cash doesn’t belong to the GameObject. It belongs to the script that you attached to the GameObject:
gameObject.Find("resourceManager(Clone)").GetComponent("YOUR_SCRIPT_NAME").Money -= 50;