Search Unity

(Closed)How to enable a disabled script?...

Discussion in 'Scripting' started by chips, Dec 14, 2007.

  1. chips

    chips

    Joined:
    Nov 30, 2007
    Posts:
    138
    Hi! I've got this 2 erros (that aparentely means the same...)
    and
    on these piece of code:
    Code (csharp):
    1.  
    2.         if (GUI.Button (Rect (0,30,200,30), button_Text1)) {
    3.             button_Text1 = "No money no honey baby!";
    4.             b_Play = false;
    5.             GetComponent(SuperMarioCamera).enabled = true;
    6.             GetComponent(Controller).enabled = true;
    7.         }
    8.  
    I also have a
    Code (csharp):
    1.  
    2. function Awake(){
    3.     GetComponent(SuperMarioCamera).enabled = false;
    4.     GetComponent(Controller).enabled = false;
    5. }
    6.  
    in the same script, but i have no idea why is this going on...

    Can anyone gimme a hand?

    TXS!
     
  2. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    Can we see the full script? You probably are doing something like calling GUI.BeginSomething(); without calling GUI.EndSomething();, or the script hits an exception and stops before reaching the EndSomething.

    One thing that looks like it could cause an exception is that you are using GetComponent() on a component that I assume (from looking at your start function) is not enabled. GetComponent() does not find unenabled components. Therefore you might be doing:

    Code (csharp):
    1.  
    2.     if (GUI.Button (Rect (0,30,200,30), button_Text1)) {
    3.          button_Text1 = "No money no honey baby!";
    4.          b_Play = false;
    5.          null.enabled = true;
    6.          null.enabled = true;
    7.       }
    8.  
    You fix this by cashing a reference to each component in a private variable in Start, then setting enabled on those variables instead of with GetComponent().
     
  3. chips

    chips

    Joined:
    Nov 30, 2007
    Posts:
    138
    Well my full code is:
    Code (csharp):
    1.  
    2. var button_Text1 : String = "Play Game";
    3. var button_Text2 : String = "Get Plug-in";
    4. var button_Text3 : String = "CREDITS";
    5. var b_Play = true;
    6.  
    7. function Awake(){
    8.     GetComponent(SuperMarioCamera).enabled = false;
    9.     GetComponent(Controller).enabled = false;
    10. }
    11.  
    12. function OnGUI () {
    13.    
    14.     if(b_Play){
    15.         // Make a group on the center of the screen
    16.         GUI.BeginGroup (Rect (Screen.width / 2 - 100, Screen.height / 2 - 120, 300, 120));
    17.         // All rectangles are now adjusted to the group. (0,0) is the topleft corner of the group.
    18.    
    19.         // We'll make a box so you can see where the group is on-screen.
    20.         GUI.Box (Rect (0,0,200,200), "PROJECT");
    21.        
    22.         if (GUI.Button (Rect (0,30,200,30), button_Text1)) {
    23.             button_Text1 = "No money no honey baby!";
    24.             b_Play = false;
    25.             GetComponent(SuperMarioCamera).enabled = true;
    26.             GetComponent(Controller).enabled = true;
    27.         }
    28.  
    29.        
    30.         if (GUI.Button (Rect (0,60,200,30), button_Text2)) {
    31.             button_Text2 = "UNITY3D.com";
    32.         }
    33.        
    34.         if (GUI.Button (Rect (0,90,200,30), button_Text3)) {
    35.             button_Text3 = "AQUIRIS";
    36.         }
    37.  
    38.         // End the group we started above. This is very important to remember!
    39.         GUI.EndGroup ();
    40.     }
    41. }
    42.  
    this was suposed to "turn On" the SuperMarioCamera and the Controller Scripts... If I change for null.enabled = true; as you said, how could I point those enables to the par of scripts?

    TXS 4 Helping!!![/code]
     
  4. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    I didn't say to use null.enabled = true;, I said that right now you are using that, because GetComponent(SuperMarioCamera) returns null if SuperMarioCamera is not enabled. And I think it isn't enabled.

    Here is an example of what I was suggesting:

    Code (csharp):
    1.  
    2.  
    3. private var coolioComponent : CoolioComponent;
    4. function Start ()
    5. {
    6.     coolioComponent = GetComponent(CoolioComponent);
    7.     coolioComponent.enabled = false;
    8. }
    9.  
    10. function Update ()
    11. {
    12.     // enable the component if the a key is held down
    13.     coolioComponent.enabled = Input.GetKey("a");
    14. }
    15.  
    16.  
     
  5. chips

    chips

    Joined:
    Nov 30, 2007
    Posts:
    138
    Oww... Ok, I see! It was definitely null... The thing is that I'm trying to enable the disabled scripts... And I'm not having a good time with that.

    By the way! Thank you VERY MUCH for the help so far!!!
     
  6. chips

    chips

    Joined:
    Nov 30, 2007
    Posts:
    138