How to find out of what type a dynamically typed variable is?

A problem people new to Unity and scrpting may run in:

I have a variable.

I know it will be only assigned to a single GameObject or Component for the whole time,

I know exactly what this GameObject or Component is,

I know it will be assigned only once in function Awake and

I know that I will be doing only stuff that does not require any dynamic/inference typing.

I also know that dynamic/inference typing is much slower than static typing (even if the variable is assigned right away in function Awake and then never changes), so I want to avoid it.

What I don't know is:

What type do I have to assign to my variable, that the things I want to do with it will work?

(Example: myVariable = GameObject.Find(“Whatever”); I would assume that myVariable would be of type GameObject. However, if I work with it I get errors saying I can’t do this or that with “UnityEngine.GameObject”.)

Is there a way to find out (with a Debug.Log or something) of what type my dynamically typed variable currently is? (So that I can then assign that type statically)

Thanks & Greetz, Ky.

http://msdn.microsoft.com/en-us/library/system.object.gettype.aspx

The documentation for each function indicates the return type. For example, you mentioned GameObject.Find(), which has this prototype in the docs:

static function Find (name : string) : GameObject

i.e. it returns a GameObject, so

var myVar:GameObject = GameObject.Find("myGameObjectName");

Note that if you're using the built in code editor, highlighting a function name and hitting F1 will load the docs in your browser.

I hope that helps.

Assuming the variable is a reference type (not a direct value like "5" or "blah"), you don't even need GetType():

var foo;
foo = GameObject.Find("Cube");
print (foo);
foo = transform;
print (foo);