- Home /
Enable script from other component.
I been searching but i havent figured out how it works yet. So i want to know if you can enable a script with a script thats assigned to another object.
I have a button and i want i want, is that when i press this button it's going to disable two scripts on another gameobject.
I know you can use. GetComponent(Script).enabled = false/true;
But i only think it works if the scipt you are trying to enable is a child of the parent where the script is on.
Please post your code and not a picture of your code.
Answer by joelfivat · Jan 24, 2014 at 11:46 AM
Of course, you can.
First you need a reference to the GameObject you want to disable the script, then call GetComponent on it just like you already do.
var gameObject : GameObject;
function OnMouseUp(){
...
gameObject.GetComponent(MyScript).enabled = false;
}
If you already have a reference to the GameObject
the script is on, you could just reference the script directly... GetComponent
should be avoided at runtime, so any way you can keep track of a component yourself is better than using GetComponent
.