Search Unity

Buttons won't enable

Discussion in '2D' started by Sharpshark, Apr 29, 2016.

  1. Sharpshark

    Sharpshark

    Joined:
    Apr 24, 2016
    Posts:
    6
    Hello,
    After my car crush I'd like buttons to appear. Well I've made a script and it doesn't work and I don't know why. There was couple of wayes I tried to do this and any of them isn't working as I said. First of all in script of car movement I've written this:
    Code (csharp):
    1.  
    2. public static bool GameOver;
    3. void OnCollisionEnter2D(Collision2D col)
    4.     {
    5. if(col.gameObject.tag == "Enemy Car")
    6.         {
    7.             Destroy(gameObject);
    8.             GameOver = true;
    9.         }
    10. }
    11.  
    It's working perfectly by the way.
    Now in UiMenager I've written this:
    Code (csharp):
    1.  
    2. public void Active(){
    3.         CarMove.GameOver = true;
    4.         foreach (Button button in buttons)
    5.             button.gameObject.SetActive(true);
    6.     }
    7.  
    And this is not working.
    I've also tried this:
    Code (csharp):
    1.  
    2. public Button button;
    3. public void Active(){
    4. if(CarMove.GameOver == true)
    5. {
    6. button.gameObject.SetActive(true);
    7. }
    8.  
    Can you please help me?
     
    Last edited: Apr 29, 2016
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Please use code tags when posting snippets of code: http://forum.unity3d.com/threads/using-code-tags-properly.143875/

    There is no unity function "Active". You can use either "Awake" for when the object first loads, or "Start" for just before the game starts to update, or "OnEnable" / "OnDisable" for when the component becomes enabled or disabled ("OnEnable" is also called when the gameobject becomes active and the component defaults to enabled.)
     
  3. Sharpshark

    Sharpshark

    Joined:
    Apr 24, 2016
    Posts:
    6
    I'm kind of newbie at scripting. Can you please help me with this "OnEnable" function? What exactly should appear in code?
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807

    If you put this code on your UIManager, if you do "UIManager.enabled = true", then this function will be called:

    However it will be called when the game starts if the UIManager is already enabled when you start the game.

    Code (CSharp):
    1. private void OnEnable(){
    2.     if(CarMove.GameOver){
    3.          foreach (Button button in buttons){
    4.              button.gameObject.SetActive(true);
    5.          }
    6.     }
    7. }