Activating Scripts in another Script?
I only seem to find old solutions for this... its pretty simple. I need to get the Script itself by using GetComponent which i think did it right:
public GameObject OBJECTTHATHOLDSSCRIPT;
Component SCRIPT;
void Start () {
SCRIPT= OBJECTTHATHOLDSSCRIPT.GetComponent <NAMEOFSCRIPT> ();
}
isn't it?
And after that i can't find how to set the Script Component to active....
Answer by Expatcat · Oct 31, 2016 at 10:11 PM
By "set the script component to active" I assume you mean enable it (And that it was initially disabled).
Your use of GetComponent() is correct - though in the future I would recommend you make the variable type the name of your component as well.
public GameObject objectThatHoldsScript;
ScriptName script;
From there, once you get the component, just set it's "enabled" value to true.
void Start() {
script = objectThatHoldsScript.GetComponent<ScriptName>();
script.enabled = true;
}
Answer by oStaiko · Oct 31, 2016 at 08:07 PM
It's not "Component script", its:
SCRIPTNAME variableName; //type of SCRIPTNAME
GameObject myObj; //type of GameObject
variableName = myObj.GetComponent<SCRIPTNAME> ();
The thing to learn here, your script is practically a type of variable, like an int, string or float. It's not exactly that, and there are some differences, but for this case it's about the same.
To set that component to Active as well, just write after that:
variableName.SetActive(true); //false turns it off
You can't use "SetActive" on components - that only works for GameObject's. You need to set the "enabled" value of the component to true.
variableName.enabled = true.
Your answer
Follow this Question
Related Questions
How to set a bool from a script equal to another bool from another script? 1 Answer
Issue referencing a method that exists in a component that is only decided at runtime 1 Answer
How to get a custom component declared by variable? 1 Answer
Component as parameter 1 Answer
Difference between GetComponent 2 Answers