Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Issue with scene load

Discussion in 'Scripting' started by zerotag, Mar 9, 2016.

  1. zerotag

    zerotag

    Joined:
    Jun 20, 2015
    Posts:
    11
    Hello,

    I've got some issues designing my classes and put the code at the correct location. Somewhere I've a knot in my head.

    At the moment I've two scenes (menu and game). When in the first (menu) scene play is hit I want to switch to my second (game) scene. Works as expected.
    Either before the second scene (game) is loaded or after the scene is loaded I want to fill the scene with a prefab I've created.
    So what I've done so far:
    - I've got a class for my MainMenuScene
    Code (CSharp):
    1. public class MenuScene : OwnMonoBehaviour
    2. {
    3.     public Canvas m_quitMenu;
    4.     public Button m_startButton;
    5.     public Button m_exitButton;
    6.  
    7.     void Start()
    8.     {
    9.         m_quitMenu = m_quitMenu.GetComponent<Canvas>();
    10.         m_startButton = m_startButton.GetComponent<Button>();
    11.         m_exitButton = m_exitButton.GetComponent<Button>();
    12.  
    13.         m_quitMenu.enabled = false;
    14.     }
    15.  
    16.     public void OnExitClicked()
    17.     {
    18.         m_quitMenu.enabled = true;
    19.         m_startButton.enabled = false;
    20.         m_exitButton.enabled = false;
    21.     }
    22.  
    23.     public void OnExitNoClicked()
    24.     {
    25.         m_quitMenu.enabled = false;
    26.         m_startButton.enabled = true;
    27.         m_exitButton.enabled = true;
    28.     }
    29.  
    30.     public void OnExitYesClicked()
    31.     {
    32.         Application.Quit();
    33.     }
    34.  
    35.     public void OnPlayClicked()
    36.     {
    37.         SceneManager.LoadScene(1);
    38.     }
    39.  
    40.     void OnLevelWasLoaded(int level)
    41.     {
    42.         if (level == 0)
    43.         {
    44.             print("MenuScene was loaded!");
    45.         }
    46.     }
    47. }
    - I've got a class for my GameScene
    Code (CSharp):
    1. public class MainGameScene : OwnMonoBehaviour
    2. {
    3.  
    4.     void Start()
    5.     {
    6.         List<GameObject> stars = GalaxyHelper.CreateNewGalaxy(123456789, GalaxyType.MilkyWay, 2048, 4, 10, 10, 25);
    7.     }
    8.  
    9.     void OnLevelWasLoaded(int level)
    10.     {
    11.  
    12.  
    13.         if (level == 1)
    14.         {
    15.             print("MainGameScene was loaded!");
    16.         }
    17.     }
    18.  
    19. }
    20.  
    - I've got a static class which contains a static method CreateNewGalaxy and returns a List<GameObject>.
    Code (CSharp):
    1. public enum GalaxyType
    2. {
    3.     MilkyWay
    4. }
    5.  
    6. public class GalaxyHelper
    7. {
    8.     private static int RAND_MAX = 32767;
    9.  
    10.     private static double random(double range, int randValue)
    11.     {
    12.         {
    13.             double fRandMax = 1.0f * RAND_MAX;
    14.  
    15.             double fArea = range * range / 2;
    16.             double fP = fArea * (randValue / fRandMax);
    17.  
    18.             return range - Math.Sqrt(range * range - 2 * fP);
    19.         }
    20.     }
    21.  
    22.     private static float doubleToFloat(double inValue)
    23.     {
    24.         float result = (float)inValue;
    25.         if (float.IsPositiveInfinity(result))
    26.         {
    27.             result = float.MaxValue;
    28.         }
    29.         else if (float.IsNegativeInfinity(result))
    30.         {
    31.             result = float.MinValue;
    32.         }
    33.         return result;
    34.     }
    35.  
    36.     public static List<GameObject> CreateNewGalaxy(int galaxySeed, GalaxyType typeOfGalaxy, int maxStars, double minStarDistance, double maxStarDistance, int armCount, double armRadius)
    37.     {
    38.         List<GameObject> stars = new List<GameObject>();
    39.  
    40.         System.Random r = new System.Random(galaxySeed);
    41.  
    42.         double angularSpread = 225 / (armCount);
    43.         double armAngle = (float)((360 / armCount) % 360);
    44.         double fDeg2Rad = Math.PI / 180.0F;
    45.  
    46.         for (int i = 0; i < maxStars; ++i)
    47.         {
    48.             double fR = random(armRadius, r.Next());
    49.  
    50.             //FLOAT fQ = random(angularSpread, distr(eng)) * (distr(eng) & 1 ? 1.0 : -1.0);
    51.             double result; // correct translated?!
    52.             if ((1 & r.Next()) == 1)
    53.             {
    54.                 result = 1.0;
    55.             }
    56.             else
    57.             {
    58.                 result = -1.0;
    59.             }
    60.             double fQ = random(angularSpread, r.Next()) * result;
    61.  
    62.             double fK = 1;
    63.             double fA = (r.NextDouble() % armCount) * armAngle;
    64.  
    65.             double fX = fR * Math.Cos(fDeg2Rad * (fA + fR * fK + fQ));
    66.             double fY = fR * Math.Sin(fDeg2Rad * (fA + fR * fK + fQ));
    67.  
    68.             GameObject newStar = (GameObject)GameObject.Instantiate(Resources.Load("Star"));
    69.             newStar.transform.position = new Vector3(doubleToFloat(fX), doubleToFloat(fY), 0);
    70.             stars.Add(newStar);
    71.          
    72.         }
    73.  
    74.         return stars;
    75.     }
    76.  
    77. }
    78.  
    So where do I have to call my CreateNewGalaxy method to fill my second scene with the returned List<GameObject>?

    I tried to use OnLevelWasLoaded, but that method isn't called. Maybe that's the old function used by Application.LoadLevel? I'm using SceneManager.LoadScene.
    The Unity docs say there're delegates to get notifications when a scene as laded/unloaded. Sounds that this is what I need. But how to use them?
     
  2. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
    If the script MainGameScene is IN the 2nd scene just leave it in the start function? OnLevelWasLoaded wont be called if the script is in the scene being loaded will it ? Unless I'm misunderstanding you here.
     
  3. zerotag

    zerotag

    Joined:
    Jun 20, 2015
    Posts:
    11
    Ha! Thank's. :)

    My fault was that the script wasn't inside the scene. After I added a GameObject and added the script it's called.