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?

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:

  1. Find the "VacancyTestersParent.

  2. Find a Component with the name `VacancyTesterParentController`

  3. 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;

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.