Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Exit to menu conditions?

Discussion in 'Scripting' started by Mussimm, Jul 3, 2020.

  1. Mussimm

    Mussimm

    Joined:
    Jun 29, 2020
    Posts:
    2
    Hi guys!

    I've built a very simple platformer as a learning project and I'm trying to get this to happen:

    Character hits the exit with win conditions met >
    Character goes inactive >
    Win music and text play >
    A few seconds pass >
    Player is booted to menu

    I'm running into a scripting problem that as soon as the character is set to inactive the coroutine to wait and then boot isn't running to conclusion.

    Any suggestions that aren't going to turn this into a horrible coding frankenstein?

    Her's my current code:

    Code (CSharp):
    1. if ((other.gameObject.CompareTag("Exit")) & (count >= 5))
    2.         {
    3.             winner.Play();
    4.             winText.text = "You Win!";
    5.             StartCoroutine(WinMenu());
    6.             gameObject.SetActive(false);
    7.  
    8.             IEnumerator WinMenu()
    9.             {
    10.                 yield return new WaitForSecondsRealtime(3);
    11.                 SceneManager.LoadScene("Menu");
    12.             }
    13.            
    14.          }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,890
    Coroutines, update methods, etc... won't run on deactivated objects and disabled components.

    You should run your coroutine from a different object. Doesn't really make sense for the player object to be responsible for this anyway.

    A Singleton GameManager type object comes to mind.
     
  3. Mussimm

    Mussimm

    Joined:
    Jun 29, 2020
    Posts:
    2
    Yeah, got it working. Set the coroutine to the exit object instead. Was a bit of a messaround but is now working like a charm.