Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by $$anonymous$$ · Oct 27, 2016 at 12:56 AM · c#gameobjectgetcomponentmonobehaviour

How do I get the type of a Monobehaviour?

In my script, I have a line:

  MonoBehaviour talktoplayerscript = subjecttotalkto.transform.FindChild("talktoplayer").GetComponent<MonoBehaviour>();

where I take get the c# script belonging to a certain GameObject. How do I find the type of the Monobehaviour, so I can start calling specific functions from it?

Is this impossible, since c# is compiled?

Thanks.

Comment
Add comment · Show 5
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 ElijahShadbolt · Oct 27, 2016 at 03:48 AM 0
Share

Are you trying to get a specific type of script on the GameObject, or are you trying to check all the $$anonymous$$onoBehaviours attached to the GameObject and get each one's type?

avatar image $$anonymous$$ ElijahShadbolt · Oct 28, 2016 at 12:13 AM 0
Share

I'm going to have a game where the player can talk to multiple people. The script that deter$$anonymous$$es what they say is different for each character. The player's script calls upon the script of the character the player is talking to to return a response to input. Since there are multiple scripts that go with different players, the player can't GetComponent a specific instance of a script.

avatar image Namey5 · Oct 27, 2016 at 05:23 AM 0
Share

Is there any particular reason you need to reference it as a $$anonymous$$onoBehaviour rather than just as a class?

avatar image hexagonius · Oct 27, 2016 at 06:28 AM 0
Share

GetComponent is kind of useless if not used with a concrete type. whatever your class is called you try calling functions from, use it's name ins$$anonymous$$d.

avatar image Bip901 · Oct 27, 2018 at 02:10 PM 0
Share

$$anonymous$$ake a base class ("NPC"), and create classes that inherit from it. This way you can GetComponent<NPC>(), and not GetComponent<$$anonymous$$onoBehaviour>().

2 Replies

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

Answer by AurimasBlazulionis · Oct 27, 2016 at 10:27 AM

It is nonsense. If you get MonoBehaviour, you only have access to MonoBehaviour stuff. You will have to cast the monobehaviour to the class you wish to call functions from and it. If you need different types of scripts using the same variable, consider using Interfaces.

Comment
Add comment · Show 1 · 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 $$anonymous$$ · Oct 28, 2016 at 12:31 AM 1
Share

Thanks. I found a way to get around this snag in the program$$anonymous$$g by using Send$$anonymous$$essage. Once I have some more time to fix it, I'll find a better option. I'm working on a tight deadline- I need my prototype complete by Friday afternoon.

I'm in my high school's Computer Game Design club. One guy wants to use software called RenPy to make the game, and I want to use Unity. We decided on $$anonymous$$onday to make prototypes for the game, and on Friday we're going to show they to each other.

avatar image
1

Answer by Genei_180 · Oct 27, 2016 at 02:17 PM

Why do you need to access MonoBehaviour? I think what your trying to do is Accessing a Script on a Gameobjekt. And than call Functions on it. You can do that by :

 GameObjekt.GetComponent<ScriptClass>().IwantToCallThisFunktion();

Than you should attach the script to the Gameobjekt. And this script can have a Behaviour MonoBehaviour.

Comment
Add comment · 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

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

257 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Value doesn't change in game when updated. 2 Answers

How do I shorten a gameObject.GetComponent 1 Answer

Get and Set only work on public variable? 1 Answer

What to use instead of GameObject.Find and GetComponent 2 Answers

Using GetComponent without knowing the script name? 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