- Home /
Type of a script
Hi
I'm wondering how this works (it's c#)
GetComponent<myscriptname>();
I always thought a script would have the type Monoscript, but then one should use
GetComponent<MonoScript>();
and it wouldn't be clear which script is returned. It seems like every script has its unique type. However with a unique type this should cause an error
MonoScript myscript=someobject.GetComponent<myscriptname>();
Can someone please help me and explain what is going on there ?
They're not monoscripts - they're $$anonymous$$onoBehaviours. Substitute $$anonymous$$onoBehaviour into your last snippet and it'll give you back the first script component it finds
Answer by IJM · Nov 01, 2010 at 01:27 PM
GetComponent is a function template, if you want to know more about templates in c#: http://en.wikipedia.org/wiki/Template_(programming)
Anyways, GetComponent<MyScript>();
will return a reference to an object of type "MyScript" and that means that you need to use it like this:
MyScript MyScriptReference = GetComponent<MyScript>();//You can name MyScriptReference as you wish
I know about templates and if I understand you right every script has its own type. What is the use of $$anonymous$$onoScript then ? Can I cast one type to the other ?
A class is an expanded concept of a data structure: ins$$anonymous$$d of holding only data, it can hold both data and functions. An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable. As far as I know there is no "$$anonymous$$onoScript" class when it comes to Unity and C#. And you don't need it, every GameObjec is unique therefor it needs unique class with unique data. =)
I certainly know what a class is and I'm not sure if you're right. GameObject is a class. Not every object in the game has its unique type. They are all GameObjects with different components attached. $$anonymous$$oreover there is a class named $$anonymous$$onoScript. Read the reference.