- Home /
Variables acesssed by other scripts??? and change int values
i am now making an Rpg very basic, and i put this in var lvl : int; var hp : int; var maxhp : int; var mp : int; var maxmp : int; var atk : int; var def : int; var mag : int; var xp : int; var maxxp : int;
function OnGUI () { GUI.Label (Rect (Screen.width / 30, Screen.height / 35 + 1,220,345),"LvL: " + lvl); GUI.Label (Rect (Screen.width / 30, Screen.height / 35 + 11,220,345),"HP: " + hp + "/"+ maxhp); GUI.Label (Rect (Screen.width / 30, Screen.height / 35 + 21,220,345),"MP: " + mp + "/"+ maxmp); GUI.Label (Rect (Screen.width / 30, Screen.height / 35 + 31,220,345),"XP: " + xp + "/"+ maxxp);
}
as you can see i have made variables for max and "current" status for what its needed for what do i have to do to make fx a collider increase xp, in a different script so fx: "first person controller with this script equipped, walks into a collider, and gains 10 xp so it will show xp gained in the window?
i know this is maybe hard but hope you are up to it :D thansk alot in advance!
Answer by Mike 3 · May 20, 2010 at 03:24 PM
You need to get hold of a reference to the script doing the GUI.
First off, you probably need to get hold of the GameObject, using one of these methods: http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
Secondly, you need to get your script component from that gameobject reference: http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Components.html
Once you have that, you can pretty much just do something along the lines of:
yourScriptVariable.xp += 50;
This, however, is considered bad practice, so I would strongly suggest adding a function to increment the XP into your script, accessing it like this:
yourScriptVariable.GiveXP(50);
This has the benefit of making it very easy to pop up XP changes on screen, as well as work out if you levelled (and any actions because of this, like ding sounds or messages)
Example:
function GiveXP(addedXP : int) { if (addedXP < 0) { Debug.Log("Can't remove exp!"); return; }
xp += addedXP;
if (xp > maxxp)
{
//level up here
}
}
hmmm, im pretty new to this whole scripting, but im slowly learning, so im still pretty lost :( no idea how to make the things work you posted, how do i make a function to "increment" my xp??
i tried this:
var target : charstats; function Update () { target.xp +=2; }
and that gives me no script errors, but! how do i do it your way? the GiveXP way? can you sahre your knowledge i would be most happy! thansk alot in advance!
then i get a bunch of errors (10,5): BCE0005: $$anonymous$$ identifier: 'xp'. (11,9): BCE0005: $$anonymous$$ identifier: 'xp'. (11,14): BCE0005: $$anonymous$$ identifier: 'maxxp'.
thank you for your patience mate
are you putting the function into the same script as all your variables in the original question?
Haha! i feel so freaking stupid now hahaha! sorry man! it works thank you so so much! you got a check mark now, love ya!
Your answer
![](https://koobas.hobune.stream/wayback/20220613012056im_/https://answers.unity.com/themes/thub/images/avi.jpg)