Getting a variable from another object

Hi,

I'm a beginner in java script but I need to do something in Unity before I start to really dive into it.

I thought it would be simple. But after searching for hours, through the forums and manuals, I give up. It's either not what I wanted or I can't understand it. By the way, I've already made some stuff in Unity via scripting but this one is giving me headaches.

Basically I want to do this:

I have one object with a script that holds a public variable called "publicspeed"

I have another object with a private variable called "speed" and I want the "speed" of that object to be equal the "publicspeed" as soon as it instantiates.

Could someone please tell me how to do this?

Thanx!

In JavaScript, to make a variable accessable to other scripts, when you declare your variables, you must use the word "static" before your variable. Yours should look something like this:

static var publicSpeed : float = 0.0;

To access that variable in another script you have to use dot syntax, so "(name of the script).(name of variable)". For you this should look like this:

private var speed : float = 0.0;

function Update (){
    speed = MyPublicSpeedScript.publicSpeed;
}

That should work. Using the Update function, this updates your speed variable to the publicSpeed variable every frame. Of course you would replace "MyPublicSpeedScript" to whatever your script that sets the public speed variable is called. Remember, the variable name must perfectly match or else you will get an error. If your variable is named "publicspeed" then you will have to change my example to that, because I used "publicSpeed" (sorry that's just my way of formatting variable names).

Good Luck.