Disable script

Hi!

Is it a way to disable a script 100%? I try to disable it, but something in the script
is still working. For example destroy the player.
But not exit level. I need to disable the exit script, so the
player is not destroyed when it touch the exit before
everything is picked up on the level.

I use this code:

function Start ()
{
	//GetComponent(Exit).enabled = false;
}

Thanks :slight_smile:

The (commented) line you put there is exactly how you would disable a component. Seems like your action comes from another script. Or I didn’t get you right, in that case you would need to provide more details.

//GetComponent(Exit).enabled = false;

Should be

GetComponent(Exit).enabled = false;

The script I want to disable is in the same script which I want to disable.
Here is the whole script

var Player				: GameObject;
var sceneManager		: GameObject;			//Load the manager script for scoring
var exitLevelTimer		: int = 3;

function Start ()
{
	GetComponent(Exit).enabled = false;
}

function Update ()
{
	if (exitLevelTimer <= 0)
	{
		Application.LoadLevel ("Scene2");
		//renderer.enabled = false;
		//transform.position = Vector2(10, -8);
	}
}
function OnTriggerEnter (other : Collider)
{
	if (other.gameObject.tag == "Player")
	{
		InvokeRepeating ("ExitLevel", 1.0,1.0);
		Destroy (Player);
	}
}

function ExitLevel ()
{
	if (--exitLevelTimer == 0)
	{
	CancelInvoke("ExitLevel");
	//exitLevelTimer = 3;
	}
}

Maybe I need to disable the script from another script? I very new to this.

If I recall correctly, disabling a script just prevents Update() from being called. This fits the behavior seen here; the player is destroyed since OnTriggerEnter() still works, but the level never ends because the code for that is in Update(). At the top of all the other functions, return if the script is not enabled, like this.

if(!enabled)
   return;

So I need to put this code in the script?
I tried that, but I must do something wrong, because it’s
not working. I tried this:

var Player				: GameObject;
var sceneManager		: GameObject;			//Load the manager script for scoring
var exitLevelTimer		: int = 3;

function Start ()
{
	GetComponent(Exit).enabled = false;
}

function Update ()
{
	if (exitLevelTimer <= 0)
	{
		Application.LoadLevel ("Scene2");
		//renderer.enabled = false;
		//transform.position = Vector2(10, -8);
	}
}
if(!enabled)
	return;
function OnTriggerEnter (other : Collider)
{
	if (other.gameObject.tag == "Player")
	{
		InvokeRepeating ("ExitLevel", 1.0,1.0);
		Destroy (Player);
	}
}

function ExitLevel ()
{
	if (--exitLevelTimer == 0)
	{
	CancelInvoke("ExitLevel");
	//exitLevelTimer = 3;
	}
}

I’m sure it’s wrong :slight_smile:

Of course its wrong, you put the code in the wrong place, you can’t put a if condition outside a function/method.

if(!enabled)

    return;

function OnTriggerEnter (other : Collider)

{

    if (other.gameObject.tag == "Player")

    {

        InvokeRepeating ("ExitLevel", 1.0,1.0);

        Destroy (Player);

    }

}

should be

function OnTriggerEnter (other : Collider)

{
if(!enabled)

    return;

    if (other.gameObject.tag == "Player")

    {

        InvokeRepeating ("ExitLevel", 1.0,1.0);

        Destroy (Player);

    }

}

Thank you very much! It’s working. Now I’m happy. :slight_smile:
I’m very new to programming