Search Unity

SceneManager 5.5.2f1 temporary switch - how to disable MainScene?

Discussion in 'Scripting' started by PizzaProgram, Mar 22, 2017.

  1. PizzaProgram

    PizzaProgram

    Joined:
    Feb 12, 2014
    Posts:
    17
    Dear Programmers, ;)
    I'm testing the new SceneManager possibilities to use multiple scenes at the "same time".
    (Yet I was putting everything into one scene, with lots of booleans to decide what to show runtime.)

    My goal is to KEEP the "Main-scene", and show other scenes just temporary.
    The problem is: since I'm not unloading the main scene >
    HOW can I make it invisible and UN-interactable ? There is no such thing as MainScene.SetActive(false);

    Tried to disable the Main Camera, but it keeps being active! (Maybe a BUG?)
    I can turn off only from Unity Inspector by hand.

    Code (CSharp):
    1. // *** MainProgram.cs
    2. public static class KAMERA_ {
    3.     public static Camera MainKam;
    4.     public static Camera NoteKam;
    5. ... }
    6.  
    7.  
    8. void Start () {
    9. ...
    10.     KAMERA_.NoteKam = GameObject.Find("Note Camera" ).GetComponent<Camera>();
    11.     KAMERA_.MainKam = GameObject.Find("MainSceneCam").GetComponent<Camera>();
    12. }
    13.  
    14. // *** MenuPanel.cs with lots of buttons
    15. void Update() {
    16. ...
    17.     const string ststxt = "StatisticalScene";
    18.    if ( !SceneManager.GetActiveScene().name.Equals(ststxt)) {
    19.        var sc = SceneManager.GetSceneByName(ststxt);
    20.        if (sc != null) if (sc.isLoaded) {
    21.            KAMERA_.MainKam.enabled = false; // >> nothing happens
    22.            KAMERA_.NoteKam.enabled = false;
    23.            OtherObjects.SetActive(false); // This works fine
    24.            SceneManager.SetActiveScene(sc);
    25.            ...
    26.        }
    27.    }
    28.  
    29.    if (GUI.Button(new Rect(w * 0.49f, h / 1.45f, w * 0.20f, h * 0.15f), "Show\nStatistics")) {
    30.        SceneManager.LoadScene (ststxt, LoadSceneMode.Additive);
    31.    }
    32. }
     
    Last edited: Mar 22, 2017
  2. PizzaProgram

    PizzaProgram

    Joined:
    Feb 12, 2014
    Posts:
    17
    Can someone help me please ??? THANKS! :D
     
  3. PizzaProgram

    PizzaProgram

    Joined:
    Feb 12, 2014
    Posts:
    17
    I've thought setting the Main-monobehavior script's ENABLE property to FALSE would be an easy-solution...
    But I can not call this from outside the main class to re-enable it!
    Code (CSharp):
    1. MyMainSceneScript.enable = true; //<< ERROR, not static public property
    ... also tried to create a static void around it:
    Code (CSharp):
    1. public static void ENABL(bool e) {
    2.    this.enabled = e; // << error: can not use "this" in static...
    3. }
    So I'm out of ideas. :(
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    To clarify, when you say that you want the scene off, you just want one script off?
    If you reference the script you want off, outside of the main class, you should be able to disable/enable it by reference.
    Another option is to keep a static Instance copy of the MainSceneScript (singleton), since you always have it loaded. Then , you can access that variable , as well as it's enabled value. Hope that makes sense. Let me know if that works/was all you needed. :)
     
  5. PizzaProgram

    PizzaProgram

    Joined:
    Feb 12, 2014
    Posts:
    17
    Thanks for answering! By "turn OFF" the main-scene I mean:
    - disable visually
    - disable update + interactivity

    How do I create a reference to MainSceneScript?

    "keep a static Instance copy of the MainSceneScript"
    - Sorry, I don't understand. Can you link an example?
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, well the visual part would be another step, I suppose, but for the main script..
    Code (CSharp):
    1. // inside MyMainScript
    2. public static MyMainScript Instance;
    3.  
    4. void Awake(){
    5. Instance = this;
    6. } // there is code to prevent this spawning, again, but your situation is quite unique in that this script/object is never destroyed, reloaded, etc.. so this is enough.
    7.  
    8. // in some other script, when you want to enable/disable that script:
    9. MyMainScript.Instance.enabled = true; // or false
    Code (CSharp):
    1. // the other concept would have been, in other scripts where you want to reference it...
    2. MyMainScript myMainScript;
    3. // then you'd either have to link it in via the inspector, or FindItByTag or something similar. but that variable could be used for the same purpose..
    Hope that makes sense.
     
  7. PizzaProgram

    PizzaProgram

    Joined:
    Feb 12, 2014
    Posts:
    17
    Coool! :) Now I understand what you mean. Thank you ! I'll try those...

    Any idea, why:
    KAMERA_.MainKam.enabled = false;
    not disabling the camera? (Can be disabled only from the inspector at runtime )
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Is that the same thing as Camera.main.enabled = false? :)
    Had never tried that before, so I just ran a test and from a button click I turned off the camera.
    If you have a few different cameras, maybe have each scene script turn its own off/on with a direct reference.