Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by lhk · Nov 01, 2010 at 12:42 PM · getcomponenttype

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 ?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mike 3 · Nov 01, 2010 at 01:29 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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
Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lhk · Nov 01, 2010 at 04:48 PM 0
Share

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 ?

avatar image IJM · Nov 01, 2010 at 06:38 PM 0
Share

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. =)

avatar image IJM · Nov 01, 2010 at 06:40 PM 0
Share

Anything else? Feel free to ask =)

avatar image lhk · Nov 01, 2010 at 07:55 PM 0
Share

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.

avatar image lhk · Nov 03, 2010 at 03:34 PM 1
Share

Perfect answer. Thank you !

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Neater way to test an Enum in a different script? 1 Answer

How could you access a script of varying name? 5 Answers

C# GetComponent Issue 2 Answers

Simple question about OnMouseButtonAsUp 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges