Using a Variable over two Scripts

I have 2 scripts… one for the player and one for the enemy…

i want now a vector3 variable in the enemy script, that i can also use in my player script…

how can i do that?

if the variable changed in the enemy script, it must also changed in the player script…

i hope someone can understand me :confused:


    var enemy : enemyScript; //Type is the name of the script
    function Start(){
     enemy = GameObject.Find('enemy').GetComponent(enemyScript); //The game object where enemyScript is attached is named enemy
//All public variables and functions on that script is now available from the enemy variable.
    enemy.someVector = Vector3(10,10,10);


//The variable needs to be declared inside this script
var someVector : Vector3;

This will set the var someVector to 10,10,10 on start.

Do read the manual

you can either use 1 static variable:
Static Variables - Unity Answers

or use public variables and create a function that will upadate all other scripts //not recomnded.

or create 1 manger that holds the varibale and all scripts take the values from it (almost like a static var)


hmm… i do not really understand how static helps me :confused:

i have the variables:

public Vector3 enemy;
public Vector3 reset;

in the Script “Enemy”

but even with Enemy.enemy or Enemy.reset, the Script “Player” doesn’t find it…

For those doing C# I found this following code to be the way to work.

public int myVariable;

GameObject myObject = GameObject.Find("objectInHeirarchy");
myVariable = myObject.GetComponent<myObjectScript>().myVariable;