- Home /
dynamic typing in Unity iOS: ...not a member of 'UnityEngine.Component'
Ive read here about a similar problem with dynamic typing in Unity iOS, where one doesnt get a warning just until they try to build.
I have this function for example in a script:
function RecountEmpties (){
var occupiedSquaresObserver = GameObject.Find("VacancyTestersParent").GetComponent("VacancyTesterParentController");
occupiedSquaresObserver.RecountEmpties();
}
the function above does the following things:
1) it searches for the object named "VacancyTestersParent"
2) it gets "VacancyTesterParentController.js" that is attached on this object
3) it runs the function RecountEmpties thats in this script.
It all runs ok when using Unity Remote, but when I hit Build & Run, I get:
...BCE0019: RecountEmpties is ont a member of UnityEngine.Component.
I tried following the solution of the link above (with the similar problem) to somehow modify the syntax but It didnt work (obviously I did it wrong).
Basically I hadnt heard about dynamic typing till now...
What would be the correct syntax for iOS?
Answer by Peter G · Jan 16, 2011 at 07:05 PM
Unity Remote is just streaming a low quality version of the editor to the iPhone so you will never get any iPhone specific bugs using it. That is why some shaders will work in Unity remote, but not on the actual device.
Here is what you are doing:
Find the "VacancyTestersParent.
Find a Component with the name
VacancyTesterParentController
Tell this component to
RecountEmpties()
.
I guess you figured out that you are dynamically typing. Since you did not specify a type for occupiedSquaresObserver, iOS will not let Unity determine it at runtime.
Since GetComponent returns a component, occupiedSquaresObserver is a Component type. But, if you look through the scripting reference, then you will see that Component doesn't have a RecountEmpties method.
In order to fix the problem, you have to declare a type for your object. In C#, you have to do this when you declare a variable, but js's var syntax let's you avoid it.
var occupiedSquaresObserver : VacancyTesterParentController = GameObject.Find("VacancyTestersParent").GetComponent("VacancyTesterParentController") as VacancyTesterController;
thank so much Peter! That fixed it. I also found the following link that one can read about this matter: http://unity3d.com/support/documentation/ScriptReference/index.Performance_Optimization.html
Answer by Jessy · Jan 16, 2011 at 08:05 PM
You can cut down on code, and you improve performance, by using:
var occupiedSquaresObserver = GameObject.Find("VacancyTestersParent").GetComponent.<VacancyTesterParentController>();
In JavaScript, occupiedSquaresObserver will automatically store a VacancyTesterParentController , so you don't need a colon.