Set variable in script from a different script...

Hey guys,

I was wondering, how do i set the variable of a script from a different one…?

I know i have to use something like this:

var objectWithOtherScript : GameObject;

function Update(){

objectWithOtherScript.GetComponent("scriptName")..........

}

but i don’t know what to do after that…

Any help would be greatly appreciated!!!

-Grady

Please use search in the future, this has been asked and answered many times. Second of all, you ussually want to use GetComponent.< T >() instead of GetComponent( T );

Say you have script1 with only one line in it:

var someVariable : int;

To change this variable from scrip2 you write, in the second script:

objectWithOtherScript.GetComponent.< script1 >().someVariable = someNumber;

void SpawnPlayer(){
GameObject player = (GameObject) Network.Instantiate(playerPrefab, new Vector3(0f, 4f, 0f), Quaternion.identity, 0);
//set the camera
SmoothFollow script = (SmoothFollow) Camera.main.GetComponent(“SmoothFollow”);
script.target = player.transform;
}

Here’s how i did it in C#.

OK, this is nice, but how can we do this from the inspector ?

What I ant to do is exactly what is done by the AnalyticsTracker with its parameters, so we may be able to select an object and then one of its property exactly the way you do it with UnityActions but with fields instead of methods !