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. Dismiss Notice

Scenemanager to load scene, but scene is already loaded

Discussion in 'Scripting' started by jo6x, Mar 20, 2016.

  1. jo6x

    jo6x

    Joined:
    Jan 2, 2016
    Posts:
    113
    What is happening is that when I run the game I can choose to play or quit, but the scene is already beeing loaded / started.
    When pressing play it's going to be loaded again, but with colors I didn't make?
    When pressing exit I can choose yes or no, by pressing yes it doesn't quit the game.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.SceneManagement;
    4. using System.Collections;
    5.  
    6. public class MenuScript : MonoBehaviour {
    7.  
    8.     public Canvas quitMenu;
    9.     public Button startText;
    10.     public Button exitText;
    11.  
    12.     void Start()
    13.     {
    14.         quitMenu = quitMenu.GetComponent<Canvas>();
    15.         startText = startText.GetComponent<Button>();
    16.         exitText = exitText.GetComponent<Button>();
    17.  
    18.         quitMenu.enabled = false;
    19.     }
    20.  
    21.     public void ExitPress()
    22.     {
    23.         quitMenu.enabled = true;
    24.         startText.enabled = false;
    25.         exitText.enabled = false;
    26.     }
    27.  
    28.     public void NoPress()
    29.     {
    30.         quitMenu.enabled = false;
    31.         startText.enabled = true;
    32.         exitText.enabled = true;
    33.     }
    34.  
    35.     public void StartLevel()
    36.     {
    37.         SceneManager.LoadScene(0);
    38.     }
    39.  
    40.     public void ExitGame ()
    41.         {
    42.         Application.Quit();
    43.         }
    44. }
    Also when using in another script: Scene scene = SceneManager.GetActiveScene(); SceneManager.LoadScene(scene.name); it goes back to the beginning but the colors are also not what I inserted.
     
    Last edited: Mar 20, 2016
  2. ShokeR0

    ShokeR0

    Joined:
    Feb 24, 2016
    Posts:
    112
    What do you mean the colors are different, can you provide screenshot or more details?
     
  3. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Application.Quit() doesn't do anything if you run from editor. But if you were to build the game package and run that, it would work. But here's a workaround that works even in editor:
    Code (CSharp):
    1.     public void ExitGame() {
    2.         #if UNITY_EDITOR
    3.         UnityEditor.EditorApplication.isPlaying = false;
    4.         #elif UNITY_WEBPLAYER
    5.         //Application.OpenURL(webplayerQuitURL);
    6.         #else
    7.         Application.Quit();
    8.         #endif
    9.     }
    About the colors, not sure if this is what you are experiencing, but it's a bug in Unity since long time. http://answers.unity3d.com/questions/919940/applicationloadlevel-changes-lighting-for-some-rea.html
     
    jo6x and Ryiah like this.
  4. jo6x

    jo6x

    Joined:
    Jan 2, 2016
    Posts:
    113
    Thanks for your reply. Zaffis got the answer to it, if you want to you can read his reply.
     
  5. jo6x

    jo6x

    Joined:
    Jan 2, 2016
    Posts:
    113
    Great thanks Zaffis, you really got that wright. it all is clear to me and it all helped a lot. :)