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
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.
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! (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.
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
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);
}
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?
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.