Search Unity

How can i load a specific scene ?

Discussion in '2D' started by dafuq313, Mar 31, 2016.

  1. dafuq313

    dafuq313

    Joined:
    Mar 29, 2016
    Posts:
    26
    I have a scene for menu and a scene for game.
    When i press start i want to load the game scene.
    When i die i want to load the menu scene.
    How can i do that ?
    My scenes are named "p" and "Menu".
    I know that if i write SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    it will load the current scene over and over again but i dont want that,i want to switch between scenes how i like.
     
  2. Donislaw

    Donislaw

    Joined:
    Mar 14, 2016
    Posts:
    7
    Hello, i'm too newbie, but i will try to help you.
    You can wrrite code like that: (c# script)

    Code (CSharp):
    1.  
    2. using UnityEngine.SceneManagement;
    3.  
    4. public void loadlevel(string level)
    5. {
    6. SceneManager.LoadScene(level);
    7.  
    8. }
    and assign it to empty object, then assign it (the object with script) to button on click, and put a name of the scene You want to load, when You will click the button, it will load the scene You want.

    Also if You want to change scene to gameover, etc. You can use something like that

    Code (CSharp):
    1.  
    2. using UnityEngine.SceneManagement;
    3.  
    4. if (life == 0)
    5. {
    6. SceneManager.LoadScene("gameover");
    7.  
    8. }
    If You don't understand something, feel free to ask question, i will try to help.
     
  3. dafuq313

    dafuq313

    Joined:
    Mar 29, 2016
    Posts:
    26
    Thx it worked.How can i put a scene to run first before the other ? because when i press play,they just run both.
     
    Last edited: Mar 31, 2016
  4. Donislaw

    Donislaw

    Joined:
    Mar 14, 2016
    Posts:
    7
    Hello, i'm glad i could help, idk if i understand it, but if you mean to select which scene should run first at game.
    You go to File---> Build Settings ---> You put first main menu scene, then you put a other scene, etc. And the main menu should be the first scene you see (atleast i think it work like that).

    Also you can deactivate game objects by :
    gameObject.SetActive
     
    Virtesia likes this.
  5. salkin99

    salkin99

    Joined:
    May 29, 2015
    Posts:
    2
    there should be a function to activate and deactivate some scenes, so the scene loads while you're in another scene or you can unload it, if you don't need it.
    Should look like this:

    SceneManager.LoadSceneAsync to load the Scene in the background

    SceneManager.UnloadScene to unload a scene

    SceneManager.SetActiveScene to switch between scenes
     
    shivamsaksham2000 and ColdMud like this.
  6. applestewart

    applestewart

    Joined:
    Dec 10, 2017
    Posts:
    4
    Uhh excuse me, but how do you load a scene when you press a button?;)
     
  7. martialartistmichael

    martialartistmichael

    Joined:
    Dec 11, 2017
    Posts:
    15
    @applestewart

    I am still learning as well but;

    In your main screen create a Empty Game Object
    Attached a C# Script to it

    In the script place;
    Code (CSharp):
    1. using UnityEngine.SceneManagement;
    2.  
    3. public class ScreenChange : MonoBehavior {
    4.  
    5.      public static ScreenChange instance;
    6.    
    7.      void Awake() {
    8.           MakeSingleton();
    9.      }
    10.  
    11.      void MakeSingleton() {
    12.           if(instance !=null) {
    13.                destroy(gameObject);
    14.           } else {
    15.                instance = this;
    16.                DontDestroyOnLoad(gameObject);
    17.           }
    18.      }
    19.  
    20.      public void GoToGameplay() {
    21.           SceneManager.LoadScene("SceneName")
    22.      }
    23.  
    24. }
    25.  
    26.  
    Once that is in add to your button an action on click and attached the GameObject. Then Select the GoToGameplay as the action.

    Still learning so please forgive me if the explanation isn't correct (believe it is), or can be done more effectively.
     
  8. Zchary

    Zchary

    Joined:
    Oct 9, 2019
    Posts:
    2
    you can use:
    Code (CSharp):
    1. SceneManager.LoadScene("SceneName")
    SceneManager.LoadScene("SceneName")
    but be sure that your scene is added in the build setting(File->Build Settings).
     
  9. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    You can make a ButtonScript and attach it to your canvas or button itself if you want.

    Code (CSharp):
    1. public void LoadSceneButton()
    2. {
    3. SceneManager.LoadScene("YourSceneName");
    4. }
    Then click on your button and scroll down to OnClickEvent() and click the plus sign +
    Drag whatever object you put the ButtonScript on into the field. Click the drop menu and select ButtonScript, and then choose the function, which in my example is LoadSceneButton().

    You button will now fire the event on click. Congrats! Your button changes levels!
     
  10. GBrooks_7

    GBrooks_7

    Joined:
    May 3, 2020
    Posts:
    1
    i want to ask how do you do this after a collision? (2D)
     
  11. chetansky98

    chetansky98

    Joined:
    Jul 7, 2020
    Posts:
    3
    Hello
    I am newbie can someone help me?
    I have made a "RestartMenu" scene for when the player dies it occurs but how do i Load the current Scene in which player dies as i multiple scenes and i want to load from RestartMenu's

    "Restart" Button
     
    Last edited: Aug 13, 2020
  12. Vegastrikes

    Vegastrikes

    Joined:
    Jun 15, 2020
    Posts:
    5
    When the player dies, get the current index of the scene

    Die()
    {
    ...
    ...
    currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    }

    On "Restart" Button

    SceneManager.LoadScene(currentSceneIndex);
     
  13. chetansky98

    chetansky98

    Joined:
    Jul 7, 2020
    Posts:
    3

    i did as you said but it is giving me errors in both scripts

    Assets\Scripts\MainMenu.cs(18,32): error CS0103: The name 'currentSceneIndex' does not exist in the current context
    Assets\Scripts\Player.cs(23,3): error CS0103: The name 'currentSceneIndex' does not exist in the current context
     

    Attached Files:

  14. Vegastrikes

    Vegastrikes

    Joined:
    Jun 15, 2020
    Posts:
    5
    Code (CSharp):
    1. public class Player : MonoBehaviour {
    2.  
    3.  
    4.     public int currentSceneIndex;
    5.  
    6. }
    define it on your Player.cs
     
  15. chetansky98

    chetansky98

    Joined:
    Jul 7, 2020
    Posts:
    3
    Thank u for concerning my problem and reply for it and also sorry for disturbing you
    But i found another way before your reply.I would like to share it. Here's the code i found.

    public void RestartGame() // Put it on Restart Button
    {
    SceneManager.LoadScene(SceneManager.GetActiveScene().name); //this will check the current scene's name and load it
    }
     
    drakangel1988 likes this.
  16. Vegastrikes

    Vegastrikes

    Joined:
    Jun 15, 2020
    Posts:
    5
    No problem. I thought that your MainMenu cs was only on your Menu scene.
    So the active scene would be menu scene etc.

    if its working now one way or another, happy to hear ! :)
     
  17. kokohassan

    kokohassan

    Joined:
    Dec 15, 2020
    Posts:
    1
    I have a question. Do I put the two scenes together in the hierarchy
     
  18. Zedimasterali

    Zedimasterali

    Joined:
    Mar 17, 2021
    Posts:
    2
    Hi, I have a question. So in my game, there are collectables that you have to collect before the time runs out, I have created a losing scene but I want to create a win scene. plz help,this is my losing scene code
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class CountDown : MonoBehaviour
    {
    public string LevelToLoad;
    public float timer;
    private Text timerseconds;

    // Start is called before the first frame update
    void Start()
    {
    timerseconds = GetComponent<Text>();
    }

    // Update is called once per frame
    void Update()
    {
    timer -= Time.deltaTime;
    timerseconds.text = timer.ToString("f0");
    if(timer <=0 )
    {
    Application.LoadLevel(LevelToLoad);
    }


    }
    }