- Home /
Update Variables from script to script?
How can I update variables on a script located on the player from a script located on an object? I've read about setters and getters but haven't seen a simple example beyond pseudo code, or maybe that's even the wrong approach. Suggestions welcome!
Answer by flokkienathur · Aug 25, 2012 at 06:01 PM
Well, first you need to get the player object (by using for example "GameObject.FindGameObjectWithTag(tag)) then you use "playerVariable.GetComponent(scriptName)", and hou store it as Script or something, Than "script.variable = key" function Update() { var player : GameObject = GameObject.FindGameObjectWithTag("player"); var scr : YourScript = player.GetComponent(YourScript); Src.YourVariable = whatevervalue; }
Isn't their a way to use something to the effect of #include to gain access to variables stored in memory? I wish I knew the ter$$anonymous$$ology for this better, and for that I do apologize.
for example, if I have a script storeInfo.cs
void playerInfo() { private int xp; }
then a second script addOneXP.cs
{ #include storeInfo
playerInfo.xp = playerInfo.xp + 1; }
Wouldn't addOneXP.cs change the value of xp on storeInfo.cs without having to call the actual object?
If anyone knows the term that I need to be looking for, let me know. Forgetting is a bitch =)
You could just make a static var xp; in the playerInfo, than. You can accses it by playerInfo.xp