Search Unity

Using Yield Between Scenes

Discussion in 'Scripting' started by boowman, Aug 13, 2014.

  1. boowman

    boowman

    Joined:
    Jan 3, 2014
    Posts:
    57
    In my game I have 2 scenes (MainMenu and Game) and in each level I have a game object that holds this code.
    Code (CSharp):
    1.  
    2. //MainMenu
    3.     public GameObject mainObjects;
    4.     public GameObject mainMenu;
    5.     public GUIText loading;
    6.  
    7.     void Awake()
    8.     {
    9.         Screen.orientation = ScreenOrientation.Portrait;
    10.     }
    11.  
    12.     void Start()
    13.     {
    14.         StartCoroutine(WaitFewSeconds(2));
    15.     }
    16.  
    17.     IEnumerator WaitFewSeconds(float waitTime)
    18.     {
    19.         loading.text = "Loading...";
    20.  
    21.         yield return new WaitForSeconds(waitTime);
    22.  
    23.         gameObject.SetActive(false);
    24.         mainObjects.SetActive(true);
    25.         mainMenu.SetActive(true);
    26.     }
    27. //Game
    28.     //Object to enable
    29.     public GameObject mainObjects;
    30.     public GameObject terrain;
    31.     public GUIText loading;
    32.     void Awake()
    33.     {
    34.         Screen.orientation = ScreenOrientation.LandscapeLeft;
    35.     }
    36.  
    37.     void Start()
    38.     {
    39.         StartCoroutine(WaitFewSeconds(2));
    40.     }
    41.  
    42.     IEnumerator WaitFewSeconds(float waitTime)
    43.     {
    44.             loading.text = "Loading...";
    45.         yield return new WaitForSeconds(waitTime);
    46.             gameObject.SetActive(false);
    47.             mainObjects.SetActive(true);
    48.             terrain.SetActive(true);
    49.     }
    50.  
    The problem is that sometimes(pretty often) the game freezes at loading and the yield is not called.
    The thing is that its not when I do something or at a specific time. Sometimes it works sometimes it doesn't.
    I did tried to only using the yield and nothing else but I still had the problem.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    You might have more luck using actual Unity scenes (separate .unity files), say one called MainMenu and one called Playing.

    Each scene has its own script. The Start() method could set your portrait/landscape.

    Each scene would ONLY contain what you need for it:
    - main menu has main menu components
    - playing has game components and terrain.

    Then in code, when it's time to go from one to the other, you would use:

    Code (csharp):
    1.  
    2. Application.LoadLevel( "MainMenu");
    3.  
    If you have a main menu and handful of levels, you would choose which one to load next.

    Be sure you add all the scenes you want to the BuildSettings window.

    Kurt
     
  3. boowman

    boowman

    Joined:
    Jan 3, 2014
    Posts:
    57
    You mean to create 2 more scenes that are going to hold the portrait and landscape code with the yield in them ?
     
  4. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
  5. boowman

    boowman

    Joined:
    Jan 3, 2014
    Posts:
    57
    I fixed the problem.
    I was setting Time.timeScale to 0 in game scene and in mainmenu was still 0 that's why nothing was working.