public class GameManager : MonoBehaviour
{
[SerializeField] int moneyCount;
[SerializeField] item[] items;
int luckyNum = 7;
int luckyItemNum;
int useTime;
float playerOldMoveSpeed;
public TextMeshProUGUI playerLifeCountText;
private GameObject player;
private PlayerController playerControllerScript;
// Start is called before the first frame update
void Start()
{
player = GameObject.Find("Player");
playerControllerScript = player.GetComponent<PlayerController>();
playerOldMoveSpeed = playerControllerScript.moveSpeed;
}
// Update is called once per frame
void FixedUpdate()
{
// If player has lives and is unactive then set him active
if (player.activeInHierarchy == false && playerControllerScript.playerLifeCount >= 0)
{
StartCoroutine(SetPlayerActive());
}
}
public void DropItemIfLucky(Vector2 myPosition, Quaternion myRotation)
{
int luckyDrawNum = Random.Range(1, 21);
if (luckyDrawNum == luckyNum)
{
luckyItemNum = Random.Range(0, items.Length);
if (items[luckyItemNum].name == "CoinX5")
{
if(Random.Range(0,2) != 1)
{
luckyItemNum = 0;
}
}
GameObject itemCreated = Instantiate(items[luckyItemNum].itemObject, myPosition, myRotation);
}
}
public void UpdatePlayerLifeCount(int count)
{
playerLifeCountText.text = "x " + count;
}
IEnumerator SetPlayerActive()
{
yield return new WaitForSeconds(1);
player.gameObject.SetActive(true);
}
public void CoinX1Function()
{
moneyCount += 1;
Debug.Log(moneyCount);
}
public void CoinX5Function()
{
moneyCount += 5;
}
public void LifeItemFunction()
{
playerControllerScript.playerLifeCount += 1;
UpdatePlayerLifeCount(playerControllerScript.playerLifeCount);
}
public void TeaItemFunction()
{
useTime = 16;
playerControllerScript.moveSpeed = 7;
StartCoroutine(ItemUseTime());
playerControllerScript.moveSpeed = playerOldMoveSpeed;
Debug.Log("All Done");
}
public IEnumerator ItemUseTime()
{
yield return new WaitForSeconds(useTime);
}
}
[System.Serializable]
public class item
{
public string name;
public GameObject itemObject;
}
The problem is in TeaItemFunction()
When I put ItemUseTime() there it stops working
Any logic you want to run after a coroutine has yielded needs to be inside the coroutine, like this:
public void TeaItemFunction()
{
useTime = 16;
playerControllerScript.moveSpeed = 7;
StartCoroutine(ItemUseTime());
}
public IEnumerator ItemUseTime()
{
yield return new WaitForSeconds(useTime);
playerControllerScript.moveSpeed = playerOldMoveSpeed;
Debug.Log("All Done");
}
2 Likes