x


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.

more ▼

asked Jan 29 '10 at 01:02 AM

SisterKy gravatar image

SisterKy
2.4k 33 41 59

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first
more ▼

answered Jan 29 '10 at 01:17 AM

Jessy gravatar image

Jessy
15.6k 72 95 196

So I take that as a "yes, it is possible"? But as this question is ment for very beginners like myself, this link does little more than baffle me about how such an easy question can lead to so much scary code :p ^^' I still have no idea how to get the information I want... Plus this appears to explain the usage for C# only? Which is as of yet useless, as Unity2.6.1 doesn't support dynamic typing for C# yet, but only for UnityScript...

Jan 29 '10 at 02:22 AM SisterKy

It's a function you can use on anything. "Debug.Log(myVariable.GetType() );"

Jan 29 '10 at 04:44 AM Jessy

Ah, now I see! Thank you! I wasn't aware that you could use functions inside Debug.Log();

Jan 29 '10 at 11:06 PM SisterKy
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jan 29 '10 at 04:21 AM

Molix gravatar image

Molix
4.8k 16 27 66

Hmm... that's weird... as I wrote, I had assumed that it's a GameObject, but for some reason it (repeatedly) threw me errors a few days ago... unfortunatly I don't have those scripts anymore and seem to be unable to reproduce those errors... Oo hmmm... well, thank you for your answer, anyway... =)

Jan 29 '10 at 11:09 PM SisterKy
(comments are locked)
10|3000 characters needed characters left

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);
more ▼

answered Jan 29 '10 at 05:46 AM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x148

asked: Jan 29 '10 at 01:02 AM

Seen: 1729 times

Last Updated: Jan 29 '10 at 01:02 AM