Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question When I put this coroutine, the function doesn't work ??! Can someone tell why?

Discussion in 'Getting Started' started by sahilsharma220202004, May 21, 2021.

  1. sahilsharma220202004

    sahilsharma220202004

    Joined:
    Nov 18, 2020
    Posts:
    33
    Code (CSharp):
    1. public class GameManager : MonoBehaviour
    2. {
    3.     [SerializeField] int moneyCount;
    4.     [SerializeField] item[] items;
    5.     int luckyNum = 7;
    6.     int luckyItemNum;
    7.     int useTime;
    8.     float playerOldMoveSpeed;
    9.  
    10.     public TextMeshProUGUI playerLifeCountText;
    11.  
    12.     private GameObject player;
    13.     private PlayerController playerControllerScript;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         player = GameObject.Find("Player");
    19.         playerControllerScript = player.GetComponent<PlayerController>();
    20.         playerOldMoveSpeed = playerControllerScript.moveSpeed;
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void FixedUpdate()
    25.     {
    26.         // If player has lives and is unactive then set him active
    27.         if (player.activeInHierarchy == false && playerControllerScript.playerLifeCount >= 0)
    28.         {
    29.             StartCoroutine(SetPlayerActive());
    30.         }
    31.     }
    32.  
    33.     public void DropItemIfLucky(Vector2 myPosition, Quaternion myRotation)
    34.     {
    35.         int luckyDrawNum = Random.Range(1, 21);
    36.         if (luckyDrawNum == luckyNum)
    37.         {
    38.             luckyItemNum = Random.Range(0, items.Length);
    39.             if (items[luckyItemNum].name == "CoinX5")
    40.             {
    41.                 if(Random.Range(0,2) != 1)
    42.                 {
    43.                     luckyItemNum = 0;
    44.                 }
    45.             }
    46.             GameObject itemCreated = Instantiate(items[luckyItemNum].itemObject, myPosition, myRotation);
    47.         }
    48.     }
    49.  
    50.     public void UpdatePlayerLifeCount(int count)
    51.     {
    52.         playerLifeCountText.text = "x " + count;
    53.     }
    54.  
    55.     IEnumerator SetPlayerActive()
    56.     {
    57.         yield return new WaitForSeconds(1);
    58.         player.gameObject.SetActive(true);
    59.     }
    60.  
    61.     public void CoinX1Function()
    62.     {
    63.         moneyCount += 1;
    64.         Debug.Log(moneyCount);
    65.     }
    66.  
    67.     public void CoinX5Function()
    68.     {
    69.         moneyCount += 5;
    70.     }
    71.  
    72.     public void LifeItemFunction()
    73.     {
    74.         playerControllerScript.playerLifeCount += 1;
    75.         UpdatePlayerLifeCount(playerControllerScript.playerLifeCount);
    76.     }
    77.  
    78.     public void TeaItemFunction()
    79.     {
    80.         useTime = 16;
    81.         playerControllerScript.moveSpeed = 7;
    82.         StartCoroutine(ItemUseTime());
    83.         playerControllerScript.moveSpeed = playerOldMoveSpeed;
    84.         Debug.Log("All Done");
    85.     }
    86.  
    87.     public IEnumerator ItemUseTime()
    88.     {
    89.         yield return new WaitForSeconds(useTime);
    90.     }
    91. }
    92.  
    93. [System.Serializable]
    94. public class item
    95. {
    96.     public string name;
    97.     public GameObject itemObject;
    98. }
    99.  
     
  2. sahilsharma220202004

    sahilsharma220202004

    Joined:
    Nov 18, 2020
    Posts:
    33
    The problem is in TeaItemFunction()
    When I put ItemUseTime() there it stops working
     
  3. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Any logic you want to run after a coroutine has yielded needs to be inside the coroutine, like this:
    Code (CSharp):
    1. public void TeaItemFunction()
    2. {
    3.     useTime = 16;
    4.     playerControllerScript.moveSpeed = 7;
    5.     StartCoroutine(ItemUseTime());
    6. }
    7.  
    8. public IEnumerator ItemUseTime()
    9. {
    10.     yield return new WaitForSeconds(useTime);
    11.     playerControllerScript.moveSpeed = playerOldMoveSpeed;
    12.     Debug.Log("All Done");
    13. }