Search Unity

Changing level

Discussion in 'Scripting' started by Wadoren, Jul 19, 2016.

  1. Wadoren

    Wadoren

    Joined:
    Mar 13, 2016
    Posts:
    9
    Hi!
    I'm new to unity and I'm building my first game.
    Today I wanted to create an exit of my level that changes to the next one.
    Basically what I did was create an empty gameobject with a collider and I attached this script to it:
    Code (CSharp):
    1. void OnTriggerEnter (Collider other)
    2. {
    3. if (other.tag == "Player")
    4. {
    5. GameManager.instance.NextLevel();
    6. }
    7. }
    Next level is a function on the Game Manager script:
    Code (CSharp):
    1. public static GameManager instance = null;
    2. private int activeSceneNumber;
    3.  
    4. void NextLevel ()
    5. {
    6. activeSceneNumber = SceneManager.GetActiveScene().buildIndex;
    7. SceneManager.LoadScene ((activeSceneNumber + 1), LoadSceneMode.Single);
    8. }
    Both of the scripts are attached to their corresponding GameoObjects, the "detector" has a collider that is trigger, the player has the player tag and I have put the scenes in order in the Build Settings. However, whenever my Player goes though the collider I get this error: "NullReferenceException: Object reference not set to an instance of an object Detector.OnTriggerEnter (UnityEngine.Collider other) (at Assets... cs: 10)". Line 10 corresponds to : GameManager.instance.NextLevel ();.
    I've tried everythig that I came up but I don't seem to find the solution to this error.
     
    Last edited: Jul 19, 2016
  2. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    GameManager.instance is probably not being set. If you have an object with the GameManager script attached that is active and enabled in your scene, then it needs to declare itself to be the instance.
    Code (csharp):
    1. // in GameManager class
    2. private void Awake()
    3. {
    4.      instance = this;
    5. }
     
  3. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    in the game manager script, do you ever set the "instance" variable? You should have a Start function or an OnEnable function that looks like this:
    Code (CSharp):
    1. void Start()
    2. {
    3.     instance = this;
    4. }
    If you never set your instance, it will be null when you call GameManager.instance.. so you'd basically be calling null.NextLevel()
     
  4. Wadoren

    Wadoren

    Joined:
    Mar 13, 2016
    Posts:
    9
    It worked! Thanks a lot. Now it changes the level when the player enters the collider.

    However when I go to the next level it doesn't seem to be lit even though it has a directional light.
    Does anyone know the solution to this?
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  6. Wadoren

    Wadoren

    Joined:
    Mar 13, 2016
    Posts:
    9
    I searched for that problem of the light and I found some posts asking the same.
    In the end, I found the solution on this one.

    PD: Thanks LeftyRighty anyway, I love that people are willing to help, even if they are errors as silly as mine ;).
     
  7. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    It may be that your scene is lit but the GI (global illumination) messed up between scenes. I've found that sometimes when I switch scenes, the ambient light source disappears or is incorrect.