- Home /
C# - Auto increment of Scenes through Collision
Hi all,
I have a basic game setup that once player hits a specific object, the scene changes to the next one by calling CompleteLevel() which increments to next scene.
Loading the first level is no problem however once Player hits to move to next stage the following error occurs on manager.CompleteLevel():
NullReferenceException: Object reference not set to an instance of an object
PlayerMovement.OnCollisionEnter (UnityEngine.Collision playerCollide) (at Assets/Scripts/Game/PlayerMovement.cs:47)
GameManager.cs
 using UnityEngine;
 using System.Collections;
 
 public class GameManager : MonoBehaviour
 {
     //PUBLIC
     public int currentScore;
     public int highScore;
     public int currentLevel;
     public int unlockedLevel;
 
     public Rect timerRect;
 
     public GUISkin guiSkin;
     public float timer;
     private string currentTime;
 
     // Use this for initialization
     void Start ()
     {
         DontDestroyOnLoad (gameObject);
     }
     
     // Update is called once per frame
     void Update ()
     {
         UpdateCountdownTimer ();
         CheckTimer ();
     }
 
     public void CompleteLevel ()
     {
         if ((currentLevel + 1) == (Application.levelCount))
         {
             print ("YOU WIN!!!!!!!!");
         } else
         {
             currentLevel++;
             Application.LoadLevel (currentLevel);
         }
     }
 }
  
  
PlayerMovement.cs
 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour
 {
     //PUBLIC Variables
     public GameManager manager;
     //PRIVATE Variables
     private Vector3 input;
     private float maxSpeed = 1f;
     private Vector3 spawn;
 
     // Use this for initialization
     void Start () {
         //The following gets the inital start position of the player which will be used for when he dies and respawn.
         spawn = transform.position;
 
         if (gameObject.GetComponent<GameManager>() != null)
         {
             manager = gameObject.GetComponent<GameManager> ();
         }
         //The following gets a reference to GameManager the component, not the manager.
         //manager = manager.GetComponent<GameManager>();
     }
     
     // Update is called once per frame
     void Update () {
         //You can useGUILayout GetAxis instead which would make the movement less snapier 
         input = new Vector3 ( Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
 
         rigidbody.AddRelativeForce (input * moveSpeed);
         
         if (transform.position.y < -2)
         {
             PlayerDeath();
         }
     }
 
     void OnCollisionEnter (Collision playerCollide)
     {
         if ((playerCollide.gameObject.tag == "Enemy"))
         {
             PlayerDeath();
         }
         if (playerCollide.gameObject.tag == "Goal")
         {
             manager.CompleteLevel();
         }
     }
 }
There aren't 55 lines. Are Player$$anonymous$$ovement and Game$$anonymous$$anager on the same GameObject? Once the 2nd run as started, pause and look at inspector value for manager - is it correct?
Hi @getyour411. The issue is occurring @ line 47.Thanks
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                