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!

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:

Secondly, you need to get your script component from that gameobject reference:

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:


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)


function GiveXP(addedXP : int)
    if (addedXP < 0)
        Debug.Log("Can't remove exp!");

    xp += addedXP;
    if (xp > maxxp)
        //level up here