Search Unity

Button disabled when game starts

Discussion in 'Getting Started' started by FVelasco, Oct 28, 2019.

  1. FVelasco

    FVelasco

    Joined:
    Jul 12, 2019
    Posts:
    12
    I'm trying press a button every time I want to start a minigame. In the minigame, it spawns random prefab fruits (up to 10) every second. Here is the code:

    Code (CSharp):
    1. public float tiempoRespawn = 1.0f;
    2. private Vector2 limitesPantalla;
    3. public Button botonJugar;
    4. void Start()
    5. {
    6.      limitesPantalla = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
    7.      botonJugar.onClick.AddListener(ButtonJugarClicked);
    8. }
    9. void ButtonJugarClicked()
    10. {
    11.      StartCoroutine(frutas());
    12. }
    13. private void spawn(){
    14.      GameObject f = Instantiate(frutaPrefab[Random.Range(0, frutaPrefab.Length)]) as GameObject;
    15.      f.transform.position = new Vector2(-limitesPantalla.x * -2, Random.Range(-limitesPantalla.y, limitesPantalla.y));
    16. }
    17. IEnumerator frutas()
    18. {
    19.      for (int i = 0; i <= 10; i++)
    20.      {
    21.          yield return new WaitForSeconds(tiempoRespawn);
    22.          spawn();
    23.      }
    24. }
    I dont' know why, when I start the game, the button is disabled and I can't press it. I have tried to write botonJugar.interactable = true, but that doesn't solve the problem.
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    If you drop a button on brand new scene in a new project to compare, does it work? Start there
     
  3. FVelasco

    FVelasco

    Joined:
    Jul 12, 2019
    Posts:
    12
    Yes, I have tried it in a new project and scene and the button is enabled, I don't know why it isn't working in my project.
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
  5. FVelasco

    FVelasco

    Joined:
    Jul 12, 2019
    Posts:
    12
  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    It's typically proper forum etiquette to post your solution so others may benefit.
     
  7. FVelasco

    FVelasco

    Joined:
    Jul 12, 2019
    Posts:
    12
    Yes, but the solution was that I was disabling that button with other script, I have only deleted that and now it works.
     
    JeffDUnity3D likes this.
  8. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    So now you know how to debug, so it was a good thing!
     
    FVelasco likes this.