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.
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?
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.
Is there any particular reason you need to reference it as a $$anonymous$$onoBehaviour rather than just as a class?
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.
$$anonymous$$ake a base class ("NPC"), and create classes that inherit from it. This way you can GetComponent<NPC>()
, and not GetComponent<$$anonymous$$onoBehaviour>()
.
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.
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.
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.