- Home /
how to disable specific scripts in an array of scripts
hello, i would like to know how to disable specific scripts in an array of scripts, at the moment it disables all
private MonoBehaviour [] scripts;
// Use this for initialization
void Start ()
{
scripts =GetComponents<MonoBehaviour>() ;
if (Application.loadedLevel == 0) {
foreach (MonoBehaviour s in scripts) {
if (s.name!="EnableScripts"){
s.enabled=false;
}
}
}
}
Answer by gregzo · Apr 14, 2012 at 09:57 AM
You need to check the type of the script, like this:
if(typeof(s)!=EnableScripts)
{
s.enabled = false;
}
This would disable a script if it is of type EnableScripts. Don't forget a script is a class, and in UnityScript, a script's name is it's type! No need for quotes...
Edit: Also, print(s.name); would give you the name of the gameObject the script is attached to, not the script's!
error CS0246: The type or namespace name `s' could not be found. Are you missing a using directive or an assembly reference?
getting that error :(
Did you put it inside the foreach?
foreach ($$anonymous$$onoBehaviour s in scripts) { if(typeof(s)!=EnableScripts) { s.enabled = false; } }
foreach ($$anonymous$$onoBehaviour s in scripts) {
if(typeof(s)!=EnableScripts){
s.enabled=false;
}
}
Answer by theUndeadEmo · Apr 14, 2012 at 10:57 AM
ok i got it working, thanks to the hint from gregzo
if(s!=GetComponent <EnableScripts>()&& s!=GetComponent <racePoints>()){
s.enabled=false;
}
Hmm, could be much simpler I$$anonymous$$O, see my answer below, but it's in UnityScript, not C#...
Answer by gregzo · Apr 14, 2012 at 10:59 AM
scripts = GetComponents(MonoBehaviour);
for(s in scripts)
{
if (typeof(s)!=EnableScripts)
{
s.enabled=false;
}
}
This works fine (in UnityScript).
not sure why it doesn't work for me :( (using unity 3.5 beta)
why still the beta? Upgrading doesn't have many downsides! And sorry I can't help you with C# syntax for this. Hopefuly, a sharper coder will chime in!
just typed unity 3.5 and it took me to the beta page, but anyway i did manage to get it working but in a different way