Search Unity

Trouble disabling script with GetComponent

Discussion in 'Scripting' started by Holocene, Aug 27, 2011.

  1. Holocene

    Holocene

    Joined:
    Feb 21, 2009
    Posts:
    347
    I am trying to use the following code:

    Code (csharp):
    1.  
    2.  
    3. function OnTriggerEnter (other : Collider)
    4. {
    5.  
    6. if(other.collider.gameObject.tag == "FRAME")  {
    7.  
    8.     (GameObject.Find("WATERSPAWN").GetComponent("Instantiate_Random_Sec_RandomObject_WATERSPAWN")as Behaviour ).enabled = false;
    9.  
    10.                Debug.Log(" Disable ");
    11. }
    12. }
    13.  
    14.  
    15.  
    16. function OnTriggerExit ( other : Collider)
    17.  
    18. {
    19.  
    20. if(other.collider.gameObject.tag == "FRAME")  {
    21.    
    22.   (GameObject.Find("WATERSPAWN").GetComponent("Instantiate_Random_Sec_RandomObject_WATERSPAWN")as Behaviour ).enabled = true;
    23.                Debug.Log(" Disable "); 
    24.    
    25.  
    26. }
    27. }

    To enable / disable this:


    Code (csharp):
    1.  
    2. var OBJECT : Rigidbody[];
    3. var minWait = 1.0;
    4. var maxWait = 10.0;
    5.  
    6. function Start ()
    7.  
    8. {
    9.    
    10.  
    11.    while(true)
    12.     {
    13.        yield WaitForSeconds(Random.Range(minWait, maxWait));
    14.         var OBJECT : Rigidbody = Instantiate(OBJECT[Random.Range(0,OBJECT.Length)], transform.position, Quaternion.identity);
    15.             }
    16. }


    Any idea why this is not working?
    Thanks,
    Greg
     
  2. zazz0000

    zazz0000

    Joined:
    Aug 25, 2011
    Posts:
    18
    Have you tried Destroy on that component?
     
  3. Holocene

    Holocene

    Joined:
    Feb 21, 2009
    Posts:
    347
    Thanks, zazz0000, but I don't want to permanently destroy that component, just disable it until my object has left the
    collider trigger. I wonder if Start on the component that I'm looking to disable is preventing this from working?

    Thanks,

    G
     
  4. Holocene

    Holocene

    Joined:
    Feb 21, 2009
    Posts:
    347
    Is there another way to temporarily disable a script other than what I'm attempting to do with the above code?
    This works, but apparently not with function Start.

    Any ideas?
     
  5. Gigiwoo

    Gigiwoo

    Joined:
    Mar 16, 2011
    Posts:
    2,981
    I am interested to know the answer to this. Typically, what I do is use a 'paused' state and have a standard base class that supports 'isPaused' and is used by everything. Like to know if what you're after gets solved. If you solve it, please post the answer.

    Gigiwoo.
     
  6. vreference

    vreference

    Joined:
    Mar 22, 2011
    Posts:
    154
    You could put a bool in the script, flip its state to enable disable it. Put scripts contents inside an if statement that only runs when bool is true.
     
  7. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    Disabling a component does not stop the coroutines it started (the yield statement means you're using a coroutine). You can stop the coroutine by deactivating the gameObject, but activating it later won't restart the coroutine. You're better off making a dedicated function for it and using StartCoroutine and StopCoroutine or StopAllCoroutines.

    Or you can switch to an Update-based approach, in which case enabling and disabling does work.
     
    Last edited: Aug 29, 2011
  8. Holocene

    Holocene

    Joined:
    Feb 21, 2009
    Posts:
    347
    Yes, coroutine is the answer. Thanks for your help!

    -G