- Home /
Disable script.
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;
}
}
When you say "it works" what do you mean? Disabling just stops Coroutines, Update and LateUpdate. Anything else would need to test for the enabled state itself.
So this is a script that disables its self then try's to turn it self back on, if so, once the script is disabled it will no longer run when you press X so you can't turn it back on, because the script is turned off
It appears to be disabling a second script, not itself. The OP also says that the script isn't disabled - which would appear to be a reference to things like collision functions still running on it, these are not affected by enabled.
i just say that the script "serranda" is disabled if i press X, but it continue to run also if is disabled. :)
Answer by edve98 · Mar 12, 2014 at 05:49 PM
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!
i tried also this way, but nothing...
#pragma strict
var scriptToControl : Serranda;
var on : boolean;
function Start () {
scriptToControl = GetComponent("Serranda");
scriptToControl.enabled = false;
}
function Update() {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.X)) {
on = !on;
if (!on){
scriptToControl.enabled = false;
}
if (on){
scriptToControl.enabled = true;
}
}
}
Could you please post your serranda script? I think the problem is there.
O$$anonymous$$, here you go:
#pragma strict
private var decrement : float;
private var toggle : boolean;
private var ray : Ray;
private var hitInfo : RaycastHit;
var Audio : AudioClip;
function Start()
{
decrement = transform.localScale.y / 1.3f; //modificare questo valore per ridurre l'altezza.
}
function On$$anonymous$$ouseDown()
{
ray = Camera.main.ScreenPointToRay(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
if(Physics.Raycast(ray, hitInfo))
{
if(hitInfo.transform.gameObject == gameObject)
{
if(Input.Get$$anonymous$$ouseButton)
{
if(!toggle)
{
transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y - decrement, transform.localScale.z);
transform.position.y += decrement / 2;
toggle = true;
audio.clip = Audio;
audio.Play();
}
else
{
transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y + decrement, transform.localScale.z);
transform.position.y -= decrement / 2;
toggle = false;
audio.clip = Audio;
audio.Play();
}
}
}
}
}
I think that he should add if statement with static boolean variable IsON, because I think unity doesn't disable function On$$anonymous$$ouseDown. I'll post how this should look soon.
Answer by whydoidoit · Mar 10, 2014 at 04:49 PM
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.
I don't understand, i drag the script that i posted in the gameobject where there is the script to disable(Serranda). What do i do now?
Answer by NeatWolf · Mar 14, 2014 at 07:27 PM
I'm more experienced in c#, anyway: In your Serranda class declare another variable
public var serrandaEnabled = true; //or False, depending on the initial state you desire
and in every function inside Serranda, in this case function OnMouseDown(), check its value:
function OnMouseDown()
{
if (!serrandaEnabled)
return;
[rest of the existing code here]
}
now, you simply have to change the lines with
scriptToControl.enabled = false; //or true
to
scriptToControl.serrandaEnabled = false; //or true
this should solve the problem.
EDIT: Or, as suggested, simply put at the beginning inside your OnMouseDown function:
if (!enabled)
return;
It would be easier just to put:
if(!enabled) return;
And use the normal enabled function that will automatically disable many parts of the script.
Yes, indeed, that would be easier, you're totally right.
$$anonymous$$y habits somehow biased my answer :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613135903im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
whats wrong with this script. it wont work 1 Answer
Flight script problem 0 Answers
Forward movement rotated on movement script? 0 Answers
MainCharacter Enemy Following Script Problem 1 Answer
Enemy Health Damage 2 Answers