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

Question about switching scenes (Need quick answers.thanks.)

Discussion in 'Scripting' started by YMbrothers, Dec 31, 2015.

  1. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    So, I know that Application.LoadLevel() can switch scene,
    but, whenever I switch it back to the original scene, everything except public values are reset.


    I actually want a scene with instructions, I want them to open in the middle of the game.
    So, is there a way to change scene without resetting anything?(Of course, the update function stops too...)

    Plz fast, I'm in a rush...


    In addition(This question is optional to answer), how to make a Fade in/out animation?
     
  2. AssemblyBandit

    AssemblyBandit

    Joined:
    Dec 12, 2015
    Posts:
    12
    Maybe use "DontDestroyOnLoad"

    Fade:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Fade : MonoBehaviour {
    5.     private MeshRenderer meshRend;
    6.  
    7.     void Start (){
    8.         meshRend = GetComponent<MeshRenderer> ();
    9.         StartCoroutine(fadeInOut());
    10.     }
    11.  
    12.     IEnumerator fadeInOut (){
    13.     //fadein (alpha starts at 0)
    14.             while(meshRend.material.color.a < 1) {
    15.             meshRend.material.color = new Color(meshRend.material.color.r,
    16.                                                 meshRend.material.color.g,
    17.                                                 meshRend.material.color.b,
    18.                                                 meshRend.material.color.a + 0.01f);
    19.             yield return null;
    20.         }
    21.         //fadeout
    22.         while(meshRend.material.color.a > 0) {
    23.             meshRend.material.color = new Color(meshRend.material.color.r,
    24.                                                 meshRend.material.color.g,
    25.                                                 meshRend.material.color.b,
    26.                                                 meshRend.material.color.a - 0.01f);
    27.             yield return null;
    28.         }
    29.     }
    30. }
     
  3. SirNiklas

    SirNiklas

    Joined:
    Jun 7, 2015
    Posts:
    85
    Calm there, loading levels is initiating the Garbage Collector which is good how it is. You maybe want to use SceneManager.LoadScene with the "mode" parameter set to Additive which adds all scene objects to the current scene. Although its not a very good way to use scenes for "little instructions" in the middle of the game.
     
  4. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    Thanks for you guys replying so quickly, I appreciate.

    so... SceneManager.LoadScene suppose to load a new scene while playing? It won't affect the original scene, right?
    Plus, eh... how to close it? It's best if you give an example code....




    Wait a second.... SceneManager isn't existing, do I need to add that plugin code?
     
  5. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    Thanks for your quick reply.

    But.... I think this may not help...
    Because, actually, I instantiate the GameObject to be the object,
    so, if it's not destroyed, I think it'll have more than 1 of them...


    Is there a way to load a scene while the original scene is just Paused not closed?



    btw, thnx for the "fading" reply, I get some of it.
    In addition, can fading apply on images?
     
    Last edited: Dec 31, 2015
  6. SirNiklas

    SirNiklas

    Joined:
    Jun 7, 2015
    Posts:
    85
    Last edited: Dec 31, 2015
  7. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    Oh ya... I forgot to update it...

    btw, I checked and found out ApplicationLoadSceneAdditive, just as you gave me,
    but... it's making the scene like chaos.

    I mean, when I did that, it opens a scene in the original scene, and I got 2 audio listeners,
    probably because I spawned in more than 1 camera.
    Plus, I can't scroll anything in the new scene... is there a way to pause the original scene?



    Edit: I think I found a thing called TimeScale, does this help?
     
  8. SirNiklas

    SirNiklas

    Joined:
    Jun 7, 2015
    Posts:
    85
    Time.timeScale is setting the time your game runs in. Good for slow motion or pausing while in a menu. But you just use the wrong technique. Its not good to use new scenes for "short instructions", you cannot just pause a different scene while in another one. There are ways to accomplish this, but its mostly not worth it because of too much work for such a little feature.

    Application.LoadLevelAdditive is adding all objects that are in the additive scene into the current scene, so if theres a second audio listener (/second camera) there will be warnings.
     
  9. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    Yeah... that's the problem....
    So, is SceneManager the only way to open a new scene while pausing the original one?
     
  10. SirNiklas

    SirNiklas

    Joined:
    Jun 7, 2015
    Posts:
    85
    There is no way to pause a scene while opening another one. You can maybe look if Unity 5.3.x has ways for this, with the SceneManager but I doubt it.
     
  11. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    The document isn't clearly showing the usage of SceneManager...

    At least, does SceneManager close the original scene?

    Plus, the instruction is quite alot, so I think it needs a scene to put it in...
     
  12. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    This might be saying something you already know, but..

    When you load a scene, it makes the entire game run from only the scripts in that scene. In other words. The entire make-up of your running project is dependent on which scene is currently loaded. As only the GameObject's that are alive in that scene will be calling their MonoBehaviour scripts (like Update, Start, Awake, OnEnable, ect). Scenes are completely independent of each other (usually). You can to AdditiveLoadScene, but what that actually does is adds every GameObject from one scene, into another scene. So you'll have both scenes gameObjects in one scene. When you do Application.LoadLevel or some such, you're loading a new scene, with the default settings of that scene. The instance of the scene you were in previously gets completely destroyed. When you load the scene again, it'll have all it's default values. You can keep GameObjects when changing scenes by using the DontDestroyOnLoad script on the gameObject you want to keep between scenes. Other than that, game data between scenes is not kept (unless you're talking about static classes).

    Normally to load instructions and things like that, you'd make a prefab of the instruction you need, and load and destroy that prefab as required. Even if it does 'seem' like another scene, like a building, or something, you can simply load the building, Transform the camera to that section, and remove that gameObject, then move the camera back again. Generally loading a totally new scene is used for something pretty major (Like going to a completely new level).

    Hope that helps.
     
  13. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    Thanks for such a long reply...
    You're talking about concepts, which is acceptable.

    But, if I'm trying to do DontDestroyOnLoad, it'll have another issue...
    actually, the gameObject is inside the assets folder, I don't know how
    to put the exact same object into the scene, so I instead
    instantiate a clone of it and add into the scene.
    Which means: everytime I load the original scene, it'll instantiate a clone,
    what if I continuously switch scenes? There may be alot of gameObjects in there while I only need 1.
     
  14. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Don't worry the fix there is pretty simple. At the beginning on the game, whereever you like. You Instantiage your prefab from the assets folder into your scene ONCE. On that prefab, make sure you have a script attached which says

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleClass : MonoBehaviour {
    5.     void Awake() {
    6.         DontDestroyOnLoad(transform.gameObject);
    7.     }
    8. }
    as per website http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html. That'll mean whenever you load new scenes, this single instance of that prefab will exist, with all the data you've changed on it throughout any scene it's ever been in.
     
  15. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    Will it instantiate again?
    because I put the instantiate thingy in the Start function...
    I think everytime you load a scene it activates the Start function, right?


    btw, if this never worked, can you instead tell me about the Application.LoadLevelAdditive() thingy?
    Because I actually want to load an information scene(pretty big), which is clickable and scrollable.
     
  16. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    It will instantiate again. Your best bet would be to make the object a singleton. This script creates enforces a single instance:

    Code (CSharp):
    1. public class SingletonExample : MonoBehaviour
    2. {
    3.     private SingletonExample _singleton;
    4.     public SingletonExample singleton { get { if (_singleton) return _singleton; else return MakeSingleton(); } }
    5.  
    6.     void Awake()
    7.     {
    8.         if(_singleton == null)
    9.         {
    10.             _singleton = this;
    11.             DontDestroyOnLoad(gameObject);
    12.         }
    13.         else
    14.         {
    15.             Destroy(gameObject);
    16.         }
    17.     }
    18.  
    19.     private SingletonExample MakeSingleton()
    20.     {
    21.         GameObject obj = new GameObject();
    22.  
    23.         return obj.AddComponent<SingletonExample>();
    24.     }
    25.  
    26.     private void OnDestroy()
    27.     {
    28.         if (_singleton)
    29.             _singleton = null;
    30.     }
    31. }
    This means whenever you call SingletonExample.Singleton.. It'll either return the existing single instance of the object, or quickly create one. It's called lazy singleton, basically means that you don't need to worry about instantiation of the class.

    Rename things however suits you, but there's the functionality.
     
  17. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    where should I put this script? anywhere?
    Plus, is SingletonExample a plugin or it's already in the UnityEngine?

    btw, it helps alot, thnx
     
  18. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    That's a script you'll want to put on the GameObject you want to keep running between scenes. Yeah this is just regular code. It'll work on any Unity setup.

    However even calling the static method SingletonExample.Singleton, will create an instance of it.. However if you're planning on having lots of components on your persitent GameObject, you'll want to just put a version of it in the scene, or do a first Instantiate of a prefab with it attached.
     
  19. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    I rather use LoadLevelAdditive, I tried, but another issue pop up...

    First, 2 audio listeners exist, so if I want to delete the main camera in scene2,
    how can I make the Canvas in Scene2 fit in the main camera in scene1?

    Second, 2 Event System exist, how can I make both of them exist?
    Because Scene2 has a scrollbar and a button on it, so I can't delete the EventSystem in scene2..
    But I also need the EventSystem in Scene1...

    So, can you tell me how to make them exist-able in 1 scene
     
  20. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Is the help scene an actual scene or something like a Sprite/image that has all the info on it? If it is then you could have it always on the scene & enable/disable the renderer. That way you aren't loading & unloading scenes. Do a similar thing with the button, only have it active when the help 'screen' is enabled.
     
  21. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    hmm... 3 things

    1.thanks for your quick reply(within 8 minute)

    2.The help scene is a canvas with a scrollbar with VERY LONG instruction and a button...

    3. How can I enable/disable components? XD
     
  22. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    I don't wanna sound discouraging, but honestly it sounds like you should put this on hold, and go through many a few tutorials about the basics of unity as well as c# coding. You need to grasp the concepts of scenes and scripting pretty well to be able to overcome relatively simple problems such as an instructions screen, which should almost definitely not be it's very own scene regardless of how complex your game is, it should almost certainly be a series of UI elements on their own canvas that shows only when on the "instructions" part of the game scene. And your not gonna want to additive scene change because that's designed more for things like streaming in the edges of a large open game world, which would be ideal to keep in its own scene.

    Right so, learn yourself up real good on the official tutorials, maybe check out 3dbuzz.com for some good tutorials, and once you dig in deeper while guided by people who walk you through step by step, and have a stronger overall understanding of unity and it's workflow, I'd imagine you wouldn't need to ask how to do this kinda stuff anymore!

    Good luck!
     
    tedthebug likes this.
  23. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    Thanks for your long reply.

    Actually, before I ask, I checked Youtube and Google, they can't help me(Or it's just me.... being dumb),
    That's why I'm asking here... Plus, this project is in a rush...
     
  24. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    As @MD_Reptile suggested, it would be better to put this aside for a week & do the official unity tutorials in the learn section. They introduce concepts that build on previous ones, explain how they work & how to integrate them etc.

    https://unity3d.com/learn/tutorials/topics/user-interface-ui

    To disable & enable a component you create a link to it in your script & then you can access it when needed & set its status.
     
    MD_Reptile likes this.
  25. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    Well here's what you might try - go to the scene where you set up the canvas to display all the instructions and create a prefab with that canvas (just drag and drop the canvas object into a folder named prefabs in your assets folder) and then return to the game scene, where you can move that canvas prefab into the scene, and disable it's canvas component (just uncheck the canvas component). Then in your games main script, keep a public variable like "public Canvas iCanvas;" which you drag and drop the canvas out of the scene onto in the inspector. You'll need "using UnityEngine.UI;" at the top of that script. Then when your player clicks/taps/stares funny at your "show instructions" button, then enable the canvas just using "iCanvas.enabled = true;" and that should do the trick. You'll have to disable it when they exit the instructions as well.
     
    tedthebug likes this.
  26. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    Thanks for both of your replies.
    Unfortunately, I have no time to watch the whole tutorial, this project needs to be finished within 2 days.

    So, what you mean...
    I should put the these scene into 1 scene, and just enable/disable their component?
     
  27. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    What project is it that it needs to be finished in 2 days?
     
  28. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    I started this project for a month.....
    I'm here to ask because I got no more idea...
     
  29. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Fair enough, though 1 month isn't really all that long for a project. The answer is above, & yes put everything in one scene & enable/disable the help screen if you want the player to be able to access it at anytime.
     
  30. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    Finally, it worked, but an issue came out(Or it's out for a long time...)

    I got text blocking the resume button, I googled it and said that canvas group can solve it,
    but... how to solve it?
     
  31. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    You can reposition everything, look at layering, or try the canvas group (I don't know what that is). When I get stuck I re-look at the tutorials to see how they fixed it or check YouTube but I'm not in as much of a rush.

    If the initial problem is resolved you should raise the new one in a new thread so that others can also search easier when they have the same problem.
     
  32. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    I finally figure it out, thnx guys.
     
    MD_Reptile likes this.