Sending code between two objects; Help me finalise this?

I’ve come to the stage where I need to send variables/functions between two or more objects. Searching the forums brings up a whole load of posts asking the same thing. However these posts nearly always go off on another route or the author just disappears and the thread dies.
I still have a struggle to do this seemingly simple task so can I ask you Unity types out there to throw in some info on this and maybe draw a line under this query ?

To help me get to grips with this, I have the following scene.
1 Cube called Player. This Player object moves left/right with the arrow keys.

1 Large Cube called Cube. This Large Cube is positioned above the player and is meant to rotate as/when the Player is moved.
The basic scene has a script called MovePlayer attached to object Player. This script contains this code:

var speed = 5.0; 

function Update () { 

 var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed; 

 transform.Translate(x, 0, 0); 
}

Player is moved left right with this script. The larger cube called Cube has a script attached called CubeScript (empty for now)
NOW: What I want to do is make a link from the Player’s script (MovePlayer) to the larger Cube’s script (CubeScript) and make the larger Cube rotate as the Player object moves through the arrow keys.I’m pretty sure my error is a failure to strictly type the Components.
If any helpful souls out there can explain the procedure, even add the required code needed for both of the scripts, I’d be very grateful. Perhaps any future queries on this subject could then be directed to this post or even a tutorial I could put together for further clarity.

The easiest way to read variables from one object’s script to another is thus;

In one of the scripts (let’s call this scriptOne) put a static var like so:

static var moveLeft : boolean = false;

static var’s can be referenced from another script.
Now it a second script you can use this in ways like so:
if (scriptOne.moveLeft == true) { do stuff here }
Or you can set them like so:
scriptOne.moveLeft = false;

This method only works properly at this low of a level if you only have one object with that script being called. For example if the script with the static var in it is attached to more than one object, then when you call for that static var from another script you’ll get which ever one unity gives you, and that’s all. Also if you have more than one object with scripts calling a single script with a static var in it, and you’re setting that static var from different scripts, just be careful you know who’s setting what and why, etc.
There are other ways to do this, but I find if you only have one of these scripts in play, this is the easiest method.

One more thing real quick. Static Var’s can’t be modified in the Unity console. They don’t show up there. They’re meant solely to be used for coding.

I’m not sure how to make the cube rotate properly with the player’s movement, but the following is how you would set up communication between your 2 scripts.

Modified script on your Player object:

var speed = 5.0;
private var cubeScript : CubeScript;

function Awake() {
	cubeScript = gameObject.FindObjectOfType(CubeScript); //replace "CubeScript" with the name of the script attached to your cube object
}

function Update () {
 var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
 transform.Translate(x, 0, 0);
 cubeScript.RotateCube();
}

Then this code inside the script on your Cube Object:

fuction RotateCube() {
	//do stuff to rotate your cube
}

But it might just be easier to keep everything in one script since your example sounds pretty simple, like this (warning: untested code):

var speed = 5.0;
var player : GameObject;
var cube : GameObject;

function Update () {
	var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
	player.transform.Translate(x, 0, 0);
	cube.transform.Rotate(Vector3.right * Time.deltaTime); //insert your own rotate code here
}

Hi

This is with reference to the 1st reply to this post: in specific to this comment in it " For example if the script with the static var in it is attached to more than one object, then when you call for that static var from another script you’ll get which ever one unity gives you, and that’s all."

I am facing a similar issue but instead of one player I have multiple player-objects with the script (scriptOne as above) attached to each of them.

How do I go about setting a variable like moveLeft in this case? In addition, is there a way to know which player object caused scriptOne.moveLeft to turn true? Thanks.