#pragma strict?

I’m using unity on Windows -
In this doc:
http://unity3d.com/support/documentation/ScriptReference/index.Performance_Optimization.html

It mentions using #pragma strict
I added that to my js script, used exactly the same example in that link, and didnt get any sort of warning / error.

Do I need to do something to turn on pragmas?
What other pragmas are there?

Thx

Nope, just include that line at the top of your script and you should be all set.

I don’t personally know of any others off the top of my head, sorry.

…but it doesnt actually work on the PC? If it wasnt obvious from my email, expected behavior was an error getting tossed, and it wasnt.

Thx for the help!

Sorta glossed right over that now didn’t I? :stuck_out_tongue: Sorry. That’s odd, AFAIK it should work on Windows. I don’t see anything specifically in the bug base after a quick search so I’ll ping the dev/qa team to see if they have any clues (sorry, no Windows machine handy at the mo’).

I just tested this on a PC and I got the correct error when I added #pragma strict. I just did this in a blank project with 2 files.

MyScript.js:

function DoSomething()
{
	Debug.Log("I'm working");
}

Test.js:

#pragma strict
function Update () {
	var foo = GetComponent(MyScript);
	foo.DoSomething();
}

This gave me the correct error. The only reason I could see for the error to not pop up is that the compiler is somehow not getting the directive, but I’m not sure what might be causing that with your code.

EDIT:
Hey Jeff, what error does it give you? Does it tell you that foo.DoSomething(); is not a member of UnityEngine.Component?
/end EDIT

It seems like this works with user defined objects, but not things like int, float, double, String, etc.

If I try to compile this:

#pragma strict

public function Start(){
	var testInt=5;
	Debug.Log(testInt);
	var testString = "Five";
	Debug.Log(testString);
}

I get no error.

However with this, I do:

#pragma strict

public function Start(){
	var testTest = GetComponent(Test);
	Debug.Log(testTest.getTestInt());
}

“Assets/MyScript.js(5,28): BCE0019: ‘getTestInt’ is not a member of ‘UnityEngine.Component’.”

But when I change it to this:

#pragma strict

public function Start(){
	var testTest: Test = GetComponent(Test);
	Debug.Log(testTest.getTestInt());
}

It compiles just fine. The only problem is, the error message is about the function call, not the fact that it doesn’t know what object type ‘Test’ is…

I’d also like to figure out where/when/how to properly use #pragma strict, so if anyone else can figure out what is going on, I’d appreciate it too.

All of your examples are expected behavior. As an answer to your question–in a production project you should make a habit of using #pragma strict on all scripts.

The docs do cover this, but to be clear: Variables are typed on declaration if they assigned as they are declared.

This variable is of type int:

var someInt = 5;

This variable is of type float:

var someFloat = 5.0;

This variable is of the most generic type, Object:

var someObject;

And the tricky one–this variable is of type Component:

var someComponent = GetComponent(Rigidbody);

This variable is not of type Rigidbody! All GetComponent() calls return a Component. All Rigidbodies are Components, but not all Components are Rigidbodies. You cannot treat this variable as a Rigidbody; this is the magic that dynamic typing provides, and #pragma strict removes.

This is what throws people off. You cannot do GetComponent(Rigidbody).AddForce(). Instead you must cast it to type Rigidbody. Most conveniently JavaScript will attempt to cast on assignment:

var someRigidbody:Rigidbody = GetComponent(Rigidbody);
someRigidbody.AddForce(Vector3.up);

Or more awkwardly:

(GetComponent(Rigidbody) as Rigidbody).AddForce(Vector3.up);