hi what is the code for disabling a script using c sharp?
how should i apply it?
thanks in adv!
There is no way to disable a script, but you can disable the execution of specific parts of a script by setting a boolean variable and checking if it’s true/false and act accordingly.
You can set .enabled = false;
same way like in other components:
http://unity3d.com/learn/tutorials/modules/beginner/scripting/enabling-disabling-components
Components != scripts.
Components may not be scripts but a script attached to a gameobject is a component.
mgear is correct…you can enable/disable it with myScriptComponent.enabled property
Ya just remember what that does, it more or less stops things like update which unity calls by its self from being called, if you explicitly call a method on a disabled component that will still run
i got that.
but why some of my script doesnt have toggle checkbox?
Do those scripts have a update method that has working code in it?
this is the script i wanna disable.
using UnityEngine;
using System.Collections;
public class Respawn : MonoBehaviour {
// Use this for initialization
public GameObject other;
private GameObject Player;
void OnTriggerEnter2D (Collider2D other){
if (other.gameObject.tag == “Car1”) {
StartCoroutine(TimeDestroy());
StartCoroutine(DieAndRespawn());
}
if (other.gameObject.tag == “Car2”) {
StartCoroutine(TimeDestroy());
StartCoroutine(DieAndRespawn());
}
if (other.gameObject.tag == “Car3”) {
StartCoroutine(TimeDestroy());
StartCoroutine(DieAndRespawn());
}
if (other.gameObject.tag == “Car4”) {
StartCoroutine(TimeDestroy());
StartCoroutine(DieAndRespawn());
}
}
IEnumerator DieAndRespawn()
{
renderer.enabled = false;
collider2D.enabled = false;
yield return new WaitForSeconds (1.5f);
transform.position = new Vector3(0f, 0f, 0f);
transform.rotation = Quaternion.identity;
collider2D.enabled = true;
renderer.enabled = true;
}
void SpawnObject() {
Player = Instantiate (other) as GameObject;
Player.transform.position = transform.position;
}
IEnumerator TimeDestroy() {
SpawnObject ();
yield return new WaitForSeconds (1.5f);
}
}
its a workable script. however may have some flaw. pardon me for that.
just wan to disable it
it looks like you would only have to add 1 line:
void OnTriggerEnter2D(Collider2D other) {
if(!enabled) return; //add this line
then if you just set that monobehaviour’s .enabled property to false, the function won’t do anything upon collisions, and i see no reason why your other functions would be called without this one. it looks like they all have to be called from OnTriggerEnter2D so if you disable that one then the others won’t get called. (if there is a chance those other functions might get called somehow without OnTriggerEnter2D, just add that line to them as well–in fact, add it to any function that might get called.)
now all you need to do is grab that component and set .enabled = false; and it won’t do anything.
the docs say .enabled behaviours get updated and !enabled components do not get updated, which to me means it only pertains to Update() and FixedUpdate(). i am not on my own PC at the moment, otherwise i would check to see if .enabled automatically concerns itself with all other monobehaviour functions. if it does, then you wouldn’t even need to add that line. but the way the docs are written it looks like you do.
Ya there is nothing to disable in that script, since you got none of the Update methods or a Start method.
Edit: chuckles posted while I was writing this.
its working freaking fine now! thanks to you all!
@cmcpasserby got it!
@ Cpt Chuckle yep, all is called from OnTriggerEnter2d. and thanks! you solved my problem!
thanks all once again luv ya.
I don’t mean to hijack this thread (and if its advised I’ll start a new one) but can anyone tell me why my Character Controller won’t stay disabled after I tell it too. It keeps turning itself back on when i compile the game.
I have more details here.
nonlin…Can you check the entire solution for .enabled and see if somewhere, somehow it
is getting .enabled = true ?
I do enable it later on when the player is spawned but nowhere should it enable the moment I hit the play button to compile and run. Literally the moment I go to test the game it just toggle back on.
I should also note that all other scripts are disabled by default. Unless disabled scripts can have an effect? In any case I have searched the script for a .enabled and there isn’t one.
hmm…thats strange and interesting.
Will it be possible to make just a simple test project with that script(And other necessary stuff)
and test it out ? I would go step by step, adding one thing at a time until I either figure
out the bug in my code or nail it down to a Unity issue.