Im having a real headache dealing with the compiler
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âŚ
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);
}
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âŚ
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
Iâm sure Ill be back with more though, -grumbles-⌠frak!
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.