simple question...

hi all!

I am trying to access a parameter in a script via another game object.

here is a snippet from the object trying to set the variable in CarScript.

///

var Car : GameObject = GameObject.Find(“car_object”);

var CarScript : MonoBehaviour = Car.GetComponent (“CarScript”);

CarScript.speed = 10;

///

I want to set the variable ‘speed’ inside CarScript but i get an error. How do I expose variables from inside scripts attached to other objects?

thanks!

You must define CarScript as of class / type CarScript, otherwise its just a MonoBehaviour and monobehaviours don’t have those fields you are looking for

thank you… could you provide a syntax example in js?

nevermind… i got it! thanks!

var CarScript : CarScript = Car.GetComponent (“CarScript”);

Does that actually work? I figured Unity would complain about you having the same variable name as a class name.

Btw, the convention is for variable names to start with a lowercase letter, and class names to be uppercase, to prevent confusion. In addition, if you use GetComponent with the plain class name (no quotes), it’s more efficient and more reliable (if you typo the name, you’ll get an error instantly and not at runtime). So,

var carObject : GameObject = GameObject.Find("car_object");
var carScript : CarScript = carObject.GetComponent(CarScript);

There are cpu issues when using the FIND and GET COMPONENT commands especially in loops and update functions. So I think what you want is as follows:

In a script called “car.js” you have a variable called speed.

To access that variable in another script you simply add the name of the script to the variable, like this example below. In a script called “vehicle.js” you want to access the above variable speed and add one to it. In the script called “vehicle.js” that would look like this:

car.speed +=1;

This means find the variable called speed in the script called car and add one to it.

How can this work if I have not ‘Found’ the other game object? Aren’t FIND and GET COMPONENT essential?

This works for me with no errors. Just for good house keeping sake I am adjusting for the conventions however…

It works because you are finding the variable in a class - as long as you reference the class, which is your script file in this case the ‘car.js’ then it will work, test it if you don’t believe me! :slight_smile: (scripts are in effect classes).

Don’t include the .js though as part of your reference though!

As I say the FIND and GET COMPONENT are NOT recommended to be overused and definately not used in loops or update functions by the Unity team themselves.

Does this require that I make the variable ‘speed’ static?

Works fine if I do.

Also I was using the FIND and GETCOMPONENT inside the function onGUI.
Is this bad practice as well as?

OnGUI generally is a special thing on the iPhone due to its performance (and you have to disable auto layout, or it will have a serious impact on the same)

Also using it in ongui is even worse than update, because ongui is called multiple times per update from what I understand

don’t see thought why you would have to re-get it over and over again. get it once in start and store it in a variable

Thanks.

So I need to declare the FIND and GET variables as static var in Start()?

Also previously I only called the FIND and GET inside a button conditional statement…

mind,

In the case of variables you don’t need to use FIND or GET.

In the case I stated, simply follow my first post instructions - use the name of the script they are in.

Okay I shall provide a new ‘real world’ example:

In a script called ‘Control_Slippy.js’ I have the following variable set up:

public static var slippyScore: int = 0;

In another script where I want to add 100 points to that variable (becaue my character has just scored 100 points) I have the following:

Control_Slippy.slippyScore +=100;

No sign of a FIND or a GET see?

perfect thanks!

it was the static part, or lack thereof, that was messing me up.

hee no problem - glad to help. :slight_smile:

Remember in iPhone development you MUST tell Unity what the variables are, so in the example above I have set it to be an ‘int’ .

It is the same with everything in iPhone dev, you have to use what is known as strict typing, others can explain this better than I, but the above is necessary.

No, you only have to do that if there would be dynamic typing involved, but usually Unity’s type inference takes care of it.

static var slippyScore = 0;

is perfectly fine because Unity knows that’s an int. (This also applies to regular Unity–Javascript in Unity rarely involves dynamic typing; it’s strongly typed because of type inference.) Also there’s no point using “public” in Javascript, since everything is public by default.

As well, you should only use static variables if it makes sense. Otherwise you’d do

private var carScript : CarScript;

function Start () {
	carScript = GameObject.Find("car_object").GetComponent(CarScript);
}

That way you only do the Find once.

–Eric

Thanks for the corrections Eric - I am still learning Unity and JavaScript. Loving it though, even though I’m still early on the learning curve.

In the case of adjusting variables declared in another script I would have thought that it was fine to just directly use:

car.speed +=1;

Where car was the script ‘car.js’ and ‘speed’ was a variable in that script. Is that not the case then? Why would I need to identify the script using this longer method?

carScript = GameObject.Find("car_object").GetComponent(CarScript);

instead of what I wrote?

does car.speed use the Find method internally and so its more costly in cpu cycles?

It can be fine, but since static variables can only have one instance, it’s not always appropriate to use them. I don’t think it uses Find internally, but still, static variables are in fact slower.

–Eric