- Home /
how do i disable all scripts apart from this one ?
i have a rockets object , that has a script attached to it.
i want to disable the script i know i can do rocket.GetComponent("scriptname").active = false;
but is there a generic way i can disable all scripts apart from the renderer, so that it will still renderer to the screen but not do any scripts
i have tryed rocket.active = false; but that also disables the renderer part
Answer by Kryptos · Jul 30, 2012 at 07:53 AM
You were close to the solution.
Assuming your script is named YourScript*:
void OnLevelWasLoaded()
{
this.enabled = false;
}
To enable the script again, you will need to have a reference to that script on another script and then:
public YourScript yourScriptReference;
//...
void SomeMethodOnAnotherScript()
{
yourScriptReference.enabled = true;
}
Now if you want to disable all scripts on the same GameObject:
void OnLevelWasLoaded
{
MonoBehaviour[] scripts = gameObject.GetComponents<MonoBehaviour>();
foreach(MonoBehaviour script in scripts)
{
script.enabled = false;
}
}
An much easier solution would be to create a prefab for your object and disabled all scripts by default (by unchecking the "enabled" box on all scripts).
the bottom one code example is perfect for what i was after , thank you
it simple but works perfectly , the syntax is still a little odd to me , cheers
FYI the code snippets are given in C#. The syntax would be a little different for JS/US.
Answer by tomekkie2 · Dec 28, 2012 at 08:11 PM
another script, affecting all the scripts in the scene, Javascript
var scripts : MonoBehaviour[] = FindObjectsOfType(MonoBehaviour) as MonoBehaviour[];
for (var script:MonoBehaviour in scripts) {
if(script == this) continue;
script.enabled = false;
}
Answer by embero · Jul 30, 2012 at 08:33 AM
You can disable scripts this way:
gameObject.GetComponent("YourScriptName").enabled = false;
Your answer
Follow this Question
Related Questions
Another Java to C# Conversion Question!! 1 Answer
Enable/Disable GameObject Button script 1 Answer
Information about Behaviour.enabled 1 Answer
Unable to enable script 0 Answers