- Home /
accessing variables in different scripts
Hi, i need to access a variable in a script attached to a different game object. What i am trying to do is count how many of the bullets i fire hit the bullets that a cannon fires, i can get my bullets to increment every time they hit a cannon bullet, however i can not get the accumulative result of all these in a single variable it is just scattered between all the bullets that i have fired. Thanks for any help.
Answer by aldonaletto · Sep 24, 2011 at 02:29 PM
Just declare the counting variable as static. A static variable is unique: all script instances will read/write the same variable. Supposing your variable is bulletHit and is declared in a script called "ShootingScript.js" :
static bulletHit: int = 0; // declare bulletHit static
The variable can be accessed as bulletHit in this script, and from any other as ShootingScript.bulletHit:
ShootingScript.bulletHit++;