- Home /
accessing a variable from one script in another with Unity
I have two scripts:
here is the first script; health.js
var health : int = 5;
var combo : int = 6;
function Start () {
}
function Update () {
}
and here is the second script; attack.js
function Start () {
}
function Update () {
if(Input.GetKeyDown("1")) {
combo +=5;
}
}
But there is a problem - combo +=5; gives error : Assets/attack.js(10,1): BCE0005: Unknown identifier: 'combo'. so how can i solve this ? please help me I don't want these in same script i am asking this second time but i couldn't solve this and i searched this in google but again i couldn't find the solve who can write me the true codes ? i looked GetComponent but i didn't understand anything. thanks :)
Answer by lancer · Sep 19, 2013 at 05:17 PM
You need:
health.combo += 5;
EDIT:
Sorry got that wrong it's:
gameObject.GetComponent(health).combo += 5;
When accessing a variable in another script you type the script name dot the variable name.
You could use health.combo += 5; if the variable is static.
you need to remember that the scripts don't know what the variable is unless you tell it the script that has the variable.