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

How to move from scene to scene? Beginner looking for help.

Discussion in 'Community Learning & Teaching' started by Ironhide, Jan 17, 2016.

  1. Ironhide

    Ironhide

    Joined:
    May 9, 2013
    Posts:
    26
    Hi!

    Im beginner with Unity (user for 2 years..) Im making this simple RollABall type of game where you collect pieces to progress. Much like this tutorial:



    I need help scripting the scene to chance when you have collected all the pieces. So simple script to move from lvl 1 to lvl 2.
     
  2. Odhim

    Odhim

    Joined:
    Mar 5, 2015
    Posts:
    11
  3. Ironhide

    Ironhide

    Joined:
    May 9, 2013
    Posts:
    26
    Hi!

    Thx for the reply! Yes this is just what i need. Im just learning C# and JS so i dont really know how to write this down.. Do you have any help on that?
     
  4. Odhim

    Odhim

    Joined:
    Mar 5, 2015
    Posts:
    11
    You just have to save two scenes, for example "scene" and "scene2", add them in the build (File > Build Settings and "Add Current").

    You should have something like this:

    buildSettin.jpg

    And then in your script after you check if all the pieces was collected (in the tutorial, it's in PlayerController class, in the setCountText function, in the if statement) , you can put "SceneManager.LoadScene("scene2");" (dont forget to add "using UnityEngine.SceneManagement;" at the beginning of your script).
     
  5. Ironhide

    Ironhide

    Joined:
    May 9, 2013
    Posts:
    26
    Ok thx for the help! I'll try this tomorrow. Hope i get it to work.
     
  6. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    Don't bother with JS because they just dropped code autocomplete in the editor.
    My guess is that eventually it'll be tottaly dropped in favor of C#.
     
  7. Ironhide

    Ironhide

    Joined:
    May 9, 2013
    Posts:
    26
    Hey, so im beginner in C# so these might look stupid..

    But here is my PlayerController script:

    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;
    using UnityEngine.SceneManagement;

    public class PlayerController : MonoBehaviour
    {

    public float speed;
    public Text countText;
    public Text winText;

    private Rigidbody rb;
    private int count;



    void Start()
    {
    rb = GetComponent<Rigidbody>();
    count = 0;
    SetCountText ();
    winText.text = "";
    }

    void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

    rb.AddForce(movement * speed);
    }


    void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.CompareTag("PickUp"))
    {
    other.gameObject.SetActive(false);
    count = count + 1;
    SetCountText ();
    }

    }
    void SetCountText ()
    {

    countText.text = "Count: " + count.ToString();
    if (count >= 8)
    winText.text = "You Win!";
    SceneManager.LoadScene("Roller_lvl2");
    }
    }


    Im getting 6 errors. "SceneManagement does not exist in the namespace 'UnityEngine'".
    I did build the 2 scenes.
     
  8. Ironhide

    Ironhide

    Joined:
    May 9, 2013
    Posts:
    26
    Any help guys?
     
  9. Ironhide

    Ironhide

    Joined:
    May 9, 2013
    Posts:
    26
    Someone wanna help me with this? Just trying to advance to the next lvl. What am i doing wrong? (I have the latest v. of unity) Thx.

    I did chance the script a little so here is it now:

    using UnityEngine.SceneManagement;
    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;


    public class PlayerController : MonoBehaviour
    {

    public float speed;
    public Text countText;
    public Text winText;

    private Rigidbody rb;
    private int count;

    public object SceneManager { get; private set; }

    void Start()
    {
    rb = GetComponent<Rigidbody>();
    count = 0;
    SetCountText ();
    winText.text = "";
    }

    void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

    rb.AddForce(movement * speed);
    }


    void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.CompareTag("PickUp"))
    {
    other.gameObject.SetActive(false);
    count = count + 1;
    SetCountText ();
    }

    }

    void SetCountText ()
    {

    countText.text = "Count: " + count.ToString();
    if (count >= 8)
    {
    winText.text = "You Win!";
    SceneManager.LoadScene("Roller_lvl2");
    }
    }
    }


    Remember that im a beginner so i may or may not have done something important to make this work.

    AND HERE ARE THE ERROR'S IM GETTING:

     
  10. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    You have defined a public object using the name SceneManager. Try changing that into SceneManager_OBJ if you are to place an object in there to do .. stuff.
    The name is throwing off the compiler i think because it's something being used by unity.
     
  11. Ironhide

    Ironhide

    Joined:
    May 9, 2013
    Posts:
    26
    You mean like this:

    using UnityEngine.SceneManager_OBJ;
    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;


    OR LIKE THIS :

    void SetCountText ()
    {

    countText.text = "Count: " + count.ToString();
    if (count >= 8)
    {
    winText.text = "You Win!";
    SceneManager_OBJ.LoadScene("Roller_lvl2");
    }
    }
    }


    ???
     
  12. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    No, i'm talking about the
    Code (CSharp):
    1. public object SceneManager { get; private set; }
    .
    Also, use the code tags when posting code to make it readable.
     
  13. Ironhide

    Ironhide

    Joined:
    May 9, 2013
    Posts:
    26
    Hey, i dont really understand what i have to do.. Im sorry. Have chanced my script too many times and i dont know what it does anymore.. It's just not working. Anyway here is my script:


    using UnityEngine.SceneManagement;
    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;


    public class PlayerController : MonoBehaviour
    {

    public float speed;
    public Text countText;
    public Text winText;
    public object SceneManager { get; private set; }
    private Rigidbody rb;
    private int count;



    void Start()
    {
    rb = GetComponent<Rigidbody>();
    count = 0;
    SetCountText();
    winText.text = "";
    }

    void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

    rb.AddForce(movement * speed);
    }


    void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.CompareTag("PickUp"))
    {
    other.gameObject.SetActive(false);
    count = count + 1;
    SetCountText();
    }

    }

    void SetCountText()
    {

    countText.text = "Count: " + count.ToString();
    if (count >= 8)
    {
    winText.text = "You Win!";
    SceneManager.LoadScene("Roller_lvl2");
    }
    }
    }

    Thx for the effort!
     
  14. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    You have a line that says "public object SceneManager { get; private set; }"
    I've never used that myself. I think you should change the SceneManager into something else, and it should be GameObject. not object.
     
  15. Odhim

    Odhim

    Joined:
    Mar 5, 2015
    Posts:
    11
    For me, you just have to delete the line "public object SceneManager { get; private set; }".

    I've just made a quick test, it's works fine (Unity 5.3.0):
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class TestLoadScene : MonoBehaviour {
    6.     void OnGUI() {
    7.         if (GUI.Button(new Rect(30, 90, 150, 30), "switch")) {
    8.             SceneManager.LoadScene("Roller_lvl2");
    9.         }
    10.     }
    11. }
     
  16. Ironhide

    Ironhide

    Joined:
    May 9, 2013
    Posts:
    26
    Can i please have like a full script would be easyer to understand. Can you just corret mine and well see if it works?


    -


    using UnityEngine.SceneManagement;
    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;


    public class PlayerController : MonoBehaviour
    {

    public float speed;
    public Text countText;
    public Text winText;
    private Rigidbody rb;
    private int count;



    void Start()
    {
    rb = GetComponent<Rigidbody>();
    count = 0;
    SetCountText();
    winText.text = "";
    }

    void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

    rb.AddForce(movement * speed);
    }


    void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.CompareTag("PickUp"))
    {
    other.GameObject.SetActive(false);
    count = count + 1;
    SetCountText();
    }

    }

    void SetCountText()
    {

    countText.text = "Count: " + count.ToString();
    if (count >= 8)
    {
    winText.text = "You Win!";
    SceneManager.LoadScene("Roller_lvl2");
    }
    }
    }
     
  17. Odhim

    Odhim

    Joined:
    Mar 5, 2015
    Posts:
    11
    As screenname_taken said, please use the code tags when posting code to make it readable.

    You wrote "other.GameObject.SetActive(false);", it should be "other.gameObject.SetActive(false);"

    It's the only issue I can see.
    I can't test the whole script, I don't have the project.

    If you still have compilation error, please post a screenshot with the error message.
     
    Last edited: Jan 18, 2016
  18. Ironhide

    Ironhide

    Joined:
    May 9, 2013
    Posts:
    26
    I still get errors here they are:


    SceneM.jpg SceneM2.jpg
     
  19. Odhim

    Odhim

    Joined:
    Mar 5, 2015
    Posts:
    11
    Can you replace "SceneManager.LoadScene("Roller_lvl2");" by "Application.LoadLevel("Roller_lvl2");"?
     
  20. Ironhide

    Ironhide

    Joined:
    May 9, 2013
    Posts:
    26
    I dont know whats going on anymore... It's all messed up...
     
  21. Ironhide

    Ironhide

    Joined:
    May 9, 2013
    Posts:
    26
  22. Odhim

    Odhim

    Joined:
    Mar 5, 2015
    Posts:
    11
    Can you upload the Assets folder of your project?
     
  23. Ironhide

    Ironhide

    Joined:
    May 9, 2013
    Posts:
    26
    There is only Materials, Prefabs (PickUps) Scenes (Roller_lvl1 and 2) Scripts
     

    Attached Files:

  24. Ironhide

    Ironhide

    Joined:
    May 9, 2013
    Posts:
    26
    Scripts: CameraControl, PayerController and Rotator
     
  25. Odhim

    Odhim

    Joined:
    Mar 5, 2015
    Posts:
    11
    I'll check your scene, the prefabs and all the scripts. Can you please upload you folder Asset in a zip file?
     
  26. Ironhide

    Ironhide

    Joined:
    May 9, 2013
    Posts:
    26
    There you go
     

    Attached Files:

  27. Odhim

    Odhim

    Joined:
    Mar 5, 2015
    Posts:
    11
    In Player in Hierarchy, you must set in the Instpector Count Text and Win Text.

    Count Text is in the Hierarchy Canvas>Count Text.
    Win Text is in in the Hierarchy Canvas>Win Text.

    I check the change scene once you pick all the coins: it works.
     
  28. Ironhide

    Ironhide

    Joined:
    May 9, 2013
    Posts:
    26
    It still says The type or naame 'SceneManagement' does not exist in the namespace 'UnityEngine' Are you missing an assembly reference. I loosing my mind with this...
     
  29. Odhim

    Odhim

    Joined:
    Mar 5, 2015
    Posts:
    11
    Seems that you don't have the last version of Unity ...

    Delete the first line of the script PlayerController. So delete "using UnityEngine.SceneManagement;"
    Clear the console, and run the scene. Should work.
     
    Ironhide likes this.
  30. Ironhide

    Ironhide

    Joined:
    May 9, 2013
    Posts:
    26
    That worked... Thank you so much for help! Im sry that this took so long...
     
  31. Ironhide

    Ironhide

    Joined:
    May 9, 2013
    Posts:
    26
    Only thing is that the second scene (Roller_Lvl2) has different lighting than lvl1. Its a lot darker.