Hi, im doing the next code and I have a problem with the coroutine. It is not working, when I press the button to start the function “PlantarComidaUno”, it don’t wait the 5 seconds and does it inmediatly. What is wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FuncionCultivo : MonoBehaviour {
// Los recursos del jugador
public int comidaArcas;
// Las plantas que creceran
[SerializeField] GameObject plantaJoven;
[SerializeField] GameObject plantaCrecida;
// Tiempo de crecimiento de cada planta
// public float tiempoPlantaUno;
// Variables temporales
private int cultivoCreciendo;
private int cultivoMaduro;
void Update () {
// Recursos del jugador
comidaArcas = PlayerPrefs.GetInt ("recursoComidaGlobal");
}
// El jugador gasta comida para plantar
public void PlantarComidaUno() {
if ((comidaArcas >= 100) && (cultivoCreciendo == 0) && (cultivoMaduro == 0)) {
PlayerPrefs.SetInt ("recursoComidaGlobal", comidaArcas - 100);
cultivoCreciendo = 1;
plantaJoven.SetActive(true);
Debug.Log ("Plantando...");
StartCoroutine (Wait(5.0f));
Debug.Log ("PLANTADO");
cultivoCreciendo = 0;
cultivoMaduro = 1;
plantaCrecida.SetActive(true);
}
}
// El jugador recoge la comida madura
public void RecolectarComidaUno() {
if (cultivoMaduro == 1) {
cultivoMaduro = 0;
plantaJoven.SetActive(false);
plantaCrecida.SetActive(false);
PlayerPrefs.SetInt ("recursoComidaGlobal", comidaArcas + 200);
}
}
IEnumerator Wait(float tempo) {
while (true) {
yield return new WaitForSeconds (tempo);
}
}
}