Search Unity

how to disable a script temporarily?

Discussion in 'Scripting' started by AppleJuicy, Jan 28, 2015.

  1. AppleJuicy

    AppleJuicy

    Joined:
    Nov 12, 2014
    Posts:
    31
    hi what is the code for disabling a script using c sharp?
    how should i apply it?
    thanks in adv!
     
  2. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    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.
     
    Kiwasi likes this.
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,411
    angrypenguin, AppleJuicy and Kiwasi like this.
  4. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    AppleJuicy likes this.
  5. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    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
     
    AppleJuicy likes this.
  6. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    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
     
    Kiwasi likes this.
  7. AppleJuicy

    AppleJuicy

    Joined:
    Nov 12, 2014
    Posts:
    31
    i got that.
    but why some of my script doesnt have toggle checkbox?
     
  8. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    Do those scripts have a update method that has working code in it?
     
    AppleJuicy likes this.
  9. AppleJuicy

    AppleJuicy

    Joined:
    Nov 12, 2014
    Posts:
    31
    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
     
  10. Cpt Chuckles

    Cpt Chuckles

    Joined:
    Dec 31, 2012
    Posts:
    86
    it looks like you would only have to add 1 line:
    Code (csharp):
    1. void OnTriggerEnter2D(Collider2D other) {
    2.     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.
     
    AppleJuicy likes this.
  11. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    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.
     
    AppleJuicy likes this.
  12. AppleJuicy

    AppleJuicy

    Joined:
    Nov 12, 2014
    Posts:
    31
    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.
     
  13. nonlin

    nonlin

    Joined:
    Dec 12, 2013
    Posts:
    46
  14. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    nonlin..Can you check the entire solution for .enabled and see if somewhere, somehow it
    is getting .enabled = true ?
     
  15. nonlin

    nonlin

    Joined:
    Dec 12, 2013
    Posts:
    46
    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.
     
    Last edited: Jan 29, 2015
  16. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    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.