Cant figure out these errors when #pragma strict turned on!

Im having a real headache dealing with the compiler :rage:
First off, my code as is all runs fine, no errors, no bugs, everything does exactly what its suppose to do.
However when I was preforming optimization while turning “#pragma strict” on, I get really annoying errors that despite my best efforts wont go away. Sure, I can knock off #pragma strict and its all back to normal but Im trying to get as much performance out of my code as possible so I’m prepared to work through the #pragma strict errors until its cleared. But no joy as of yet… :frowning:

Example:

var RealTimeObject : Object;

function DoSomething(){

	RealTimeObject = Instantiate (SomePrefab, position, SomePrefab.transform.rotation);
	RealTimeObject.gameObject.transform.BroadcastMessage("SomeFunction", SomeValue, SendMessageOptions.DontRequireReceiver);
	RealTimeObject.gameObject.transform.BroadcastMessage("SomeFunction", 0, SendMessageOptions.DontRequireReceiver);

}

“RealTimeObject.gameObject.transform.BroadcastMessage(“SomeFunction”, SomeValue, SendMessageOptions.DontRequireReceiver);”

Gives me an error in the compiler saying “gameObject” is not a member of Object!?

There are tons of other equally annoying errors like this of which I cant figure out, but hey, might as well start with the first one…

Whats driving me nuts is that obviously the compiler figures all this out for me when #pragma strict is off, but why am I failing grrsss…:face_with_spiral_eyes:

Any help appreciated, thanks.:slight_smile:

RealTimeObject is of type Object:

Not sure what you expect it to be, but I’m thinking something inheritting from:

Unless you explicitly cast it to a type that has a gameObject you can’t use that gameObject property.

Well yes, I can set RealTimeObject to : GameObject, Transform, whatever and with #pragma strict off it isn’t an issue.

Like I said, the code does exactly what its suppose to do without any “In Game” bugs or compilers errors. But while turned on…

for example : “RealTimeObject = Instantiate (SomePrefab, position, SomePrefab.transform.rotation);”

A normal piece of code like this, pops up an error demanding RealTimeObject be declared as an “Object”, which then cascades into my first problem as mentioned previously…


“RealTimeObject.gameObject.transform.BroadcastMessage(“SomeFunction”, SomeValue, SendMessageOptions.DontRequireReceiver);”

Gives me an error in the compiler saying “gameObject” is not a member of Object!?


This is because Instantiate returns an Object.

At compile time you can’t guarantee that the thing you just instantiated is going to be a GameObject, so you can’t assign it onto a GameObject.
And if you have RealTimeObject set as an Object, then you can’t guarantee at compile time that it’s going to contain a variable called gameObject and therefore can’t call it.

Think of #pragma strict as having to just guarantee what things are at Compile time, rather than at run time. Even if you know it will work, if there is a chance you could pass a different type of object to that reference or call a function on an object that may or may not have that function, it’s going to fail you.

Yep that’s fair enough, I do understand that, thank you.

In the last few min I discovered that adding “as GameObject” to the end of “RealTimeObject = Instantiate (SomePrefab, position, SomePrefab.transform.rotation) as GameObject;”, fixes that particular error :slight_smile:

I’m sure Ill be back with more though, -grumbles-… frak!

If you’re using Unity 3.4 or later and JS, then Instantiate returns the type of the object you’re instantiating, rather than Object.

–Eric

Hi folks, newb here. Having an issue with #pragma strict.

I have a simple script attached to a cube that detects whether the cube has ever been collided…

#pragma strict
public var counted:boolean = false;

… and this is the code in the character controller that flips it…

#pragma strict
public var hitCount:int = 0;
function OnTriggerEnter(collision:Collider) {
  if(collision.gameObject.tag == "target") {
    var target:GameObject = collision.gameObject;
    if(!target.counted) {
      hitCount++;
      target.counted = true;
    }
  }
}

Error is “‘counted’ is not a member of ‘UnityEngine.GameObject’.” Most of the solutions I’ve found to this issue assume that the target object was found via GetComponent(), which isn’t the case here.

Earlier I was just trying to access collision.gameObject.counted, I added the “var target” bit to make sure I was getting explicit casting, but that didn’t help. Obviously everything here works if #pragma strict is left out.

It is indeed not a member of GameObject, it’s a member of the script you have attached to the GameObject, so you need to use GetComponent. Also, use CompareTag rather than tag == “target”, it’s more efficient.

–Eric

That worked! Thank you for taking the time to help me out.