- Home /
Turn off a script?
Hello. I know you can enable/disable a game object through something like this, how can I do that with a script (in another object)? Thanks
var CS : GameObject;
function Update (){ if (Genre==1) {CS.SetActiveRecursively(true);}; }
Answer by Kourosh · Dec 27, 2010 at 08:13 AM
you should use CS.GetComponent("ScriptName").enabled = false;
But in c# it says that GetComponent("ScriptName" doesn't have method " enabled" why so ??
$$anonymous$$ost components, including any scripts, have an "enabled" variable (gameobjects use SetActive() to turn them on/off in a similar way).
To access another script on the same gameobject:
GetComponent<ScriptName>();
You should really store that in a variable in advance (like in the Start method) since you don't want to call GetComponent on the fly if possible:
ScriptName otherScript;
void Start()
{
otherScript = GetComponent<ScriptName>();
}
Then assign the script's "enabled" variable to false:
otherScript.enabled = false;
Or, like the above answer did it all together on one line without storing it in a variable:
GetComponent<ScriptName>().enabled = false;
If the script was on another object, you'd need to use some method in front of GetComponent to access that specific gameobject. Like a variable that you've already assigned that gameobject to, or a function like GameObject.Find() or GameObject.FindWithTag(). Example:
GameObject.Find("Player").GetComponent().enabled = false;
Answer by przem997 · Apr 16, 2013 at 10:28 AM
CS.GetComponent("ScriptName").enabled = false;
Errors:
BCE0019: 'enabled' is not a member of 'UnityEngine.Component'.
Your answer
Follow this Question
Related Questions
Adding a script component through scripting 1 Answer
Why wont this work? (I've done similar 1000 times fine) 1 Answer
Problems deactivating a script. 1 Answer
remove script 2 Answers
Disable/Enable Script or Add/Remove? 1 Answer