Strictly typed variables vs. Dynamic - in an IF Statement?

Hey everyone :slight_smile:

I have come across what many of us do when a project is written for Unity3D and is opened up in Unity iPhone:

From Duck’s comment on Unity Answers: Link Here

So I learned how to break down what our programmer wrote in the Unity version:

globals.GetComponent(Globals).playerScore += targetScore;

And rearrange it to work in Unity iPhone doing the following:

var global : Globals = globals.GetComponent(Globals);
global.playerScore += targetScore;

NOW the problem I am having:

Our programmer used the “GetComponent(SOMETHING).someThingHere” line in an IF STATEMENT like so:

if(globals.GetComponent(Globals).systemOn)

Using the pattern from what I learned above I tried to fix the IF STATEMENT by doing this:

if(var global : Globals = globals.GetComponent(Globals);
    global.systemOn;)

When I did that, I got the following Unity Error:

I am not sure how to adjust the dynamically changed variable here to a strict one when it is in an IF statement that is not expecting that code.

I am sure I am missing something, as I am very new to all of this, so I accept any criticism. Appreciate the time! :smile:

what you need and are looking for is type casting instead of type inference use.

if( (bla.GetComponent(someType) as someType).somePublicVariable == "green") :slight_smile:

Thanks for the quick reply Dreamora!

I want to be sure I understand how to implement your code you gave me into our project, so let me know if this is what you meant:

Your Code:

if( (bla.GetComponent(someType) as someType).somePublicVariable == "green")

The original code with the error:

if(globals.GetComponent(Globals).systemOn)

What I need to make:

if( (globals.GetComponent(Globals) as Globals).systemOn == "green")

I admit I don’t understand what to do with the == “green” part…