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…
1 Like
2 Answers
2
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;
}
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
– oStaikoYou 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.
– Expatcat