- Home /
GetComponent(); issue
Hi so I have a script that I want to handle all interactions so when a mouse clicks something it easily knows where to go from there
The idea is that you on click the raycast finds the interaction script and then in the interaction script it will have a variable which is set Per Object as to what script it will activate, Example : If the interaction script is on a spawner the raycast will activate the interaction script and then the interaction script will activate the Spawn script
This script is obviously incomplete and is the bare minimal to show where I am heading with the code
public bool Active;
public string ScriptName;
public bool RequiresRaycast;
void Update(){
if (Active == true) {
UnknownScript = transform.parent.GetComponent<ScriptName>();
UnknownScript.Active = true;
Active = false;
Is there a way I can get this to use a string in .GetComponent? It seems quite complicated considering you need to make it a variable itself when you get it so it would mean I would have to make it literal when I write it like "ScriptName = transform.parent.GetComponent();" the first part "ScriptName" would have to be a new variable using the strings variable and not to try and reset it... God knows
Ps: the RequiresRaycast variable will be implemented later for when script clicked needs the information of where it was hit, example : Movement on terrain
Answer by zach-r-d · Jun 11, 2015 at 09:36 PM
If ScriptName is a string, you must use the non-generic version of GetComponent, and treat it as a MonoBehaviour:
MonoBehaviour UnknownScript = transform.parent.GetComponent(ScriptName) as MonoBehaviour;
I also recommend using .enabled rather than defining your own .Active as .enabled will probably do what you want already.
Yeh I think I understand what you mean here, I will probably have to do a little bit of research and a bit of tinkering but this should be able to work somehow, will keep you posted
And as for the .enable, good call, I will probably switch it out to that
So you want to find all of the scripts of a given type in the scene and enable them? Or perhaps all of the scripts of a given type in just the parent?
$$anonymous$$y Original reply to you was kind of meant for the other guy sorry, I edited it
Do you feel it would be just as efficient for me to just have several functions in my single Interaction script and then on Interaction "Start" it invokes 1 of the functions using bools for each? then I can just have in the Functions to get component of that specific script and activate it in the parent?
Example, Void Script1, Void Script2, Void Script3 <-- Script1 might get a spawn script component and activate it if bool1 = true?
I just went with using the new method by using multiple bools and the script activating it, thanks for the help though! I'm sure other people will find your answer useful
Your answer

Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Unity can't see any scripts 0 Answers
how to disable/enable a script in c#? 2 Answers
CinemachineBrain component reference 1 Answer