Dear All,
Suppose I have this script attached on many objects, and I want to disable this script using single script.
I try to use this script :
function OnMouseUp () {
var objs=GameObject.FindGameObjectsWithTag("objwithscript");
for (var obj in objs){
GetComponent("scriptOFF").enabled=false;}
}
But it seems Unity cannot find it.
Any idea or my script is maybe wrong?
Thank in advance
Try replacing this line:
GetComponent("scriptOFF").enabled=false;}
with this:
obj.GetComponent("scriptOFF").enabled=false;}
Thanks but it’s not work
But if :
function OnMouseUp () {
var objs=GameObject.FindGameObjectsWithTag("hilite");
for (var obj in objs){
obj.renderer.enabled = !obj.renderer.enabled;
}
}
It works!
So, is there any way for GetComponent(“script”).enabled =false?
You probably just need to verify the result from GetComponent in case an object does not contain it:
function OnMouseUp () {
var objs=GameObject.FindGameObjectsWithTag("objwithscript");
for (var obj in objs){
var component : scriptOFF = obj.GetComponent(scriptOFF);
if( component ) {
component.enabled=false;
}
}
}
Ya,
you absolutely right.
I just realize same thought too.
Thank you very much!
Another things,
When I attempt to tag child objects, I cannot block objects and assign it with tags at once.
I must assign each of objects with particular tag.
Is it true?
Thank again for enlightenment.
It would probably be much easier to use a static variable if this is your own script. No getting gameobjects, getting components, or looping required.
In ScriptThatTurnsThemOff.js:
MyScript.allEnabled = false;
In MyScript.js:
static var allEnabled : boolean = true;
function Update() {
if (allEnabled) {
//do what the script does
}
}