I open new question because i’ve not received any reply.
I drag this script to one gameobject with its script, the script works, because the variable scriptToControl turns on and off the required script, but the script don’t disable… it’s works despite is Off. Anyone can help me?
Thank you in advance
#pragma strict
var scriptToControl : Serranda;
function Start () {
scriptToControl = GetComponent("Serranda");
}
function Update() {
if (Input.GetKeyDown(KeyCode.X)) {
Debug.Log("User pressed X");
if (scriptToControl.enabled)
scriptToControl.enabled = false;
else
scriptToControl.enabled = true;
}
}
If you are assigning the script using the inspector then you should remove your Start function which replaces the inspector set value with the named component attached to this scripts Game Object or null if doesn’t have one.
This could be because unity doesn’t really disable all functions, for example function Update (Well, in your case it’s OnMouseDown). You can use function Start with infinite while loop or something, so it could be disabled.
If you are not using several serranda scripts, you can do like this:
in serranda script put static variable IsOn and if statement in function OnMouseDown. This should look like this:
static var IsOn : boolean = false; //change to true, if you want thias script active from start
//your other varaibles/script here till function OnMouseDown
function OnMouseDown(){
if(IsOn == true){
//your script here from function OnMouseDown
}
}
//more of your script here
and in your toggle script make just this:
function Update(){
if(Input.GetKeyDown(KeyCode.X)){
serranda.IsOn = !serranda.IsOn;
}
}
And that is it! Of course you could add more neat stuff like not using static variables and making variable witch let’s choose what script you want to control, so you can use more than one of the same script in one scene, but I’ll leave it for you. Good luck!