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:
public float tiempoRespawn = 1.0f;
private Vector2 limitesPantalla;
public Button botonJugar;
void Start()
{
limitesPantalla = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
botonJugar.onClick.AddListener(ButtonJugarClicked);
}
void ButtonJugarClicked()
{
StartCoroutine(frutas());
}
private void spawn(){
GameObject f = Instantiate(frutaPrefab[Random.Range(0, frutaPrefab.Length)]) as GameObject;
f.transform.position = new Vector2(-limitesPantalla.x * -2, Random.Range(-limitesPantalla.y, limitesPantalla.y));
}
IEnumerator frutas()
{
for (int i = 0; i <= 10; i++)
{
yield return new WaitForSeconds(tiempoRespawn);
spawn();
}
}
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.