Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by maltadirk · Mar 30, 2014 at 01:11 AM · nullreferenceexceptiongetcomponentscene-switchingapplication.loadlevel

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();
         }
     }
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Mar 30, 2014 at 01:18 AM 1
Share

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?

avatar image maltadirk · Mar 30, 2014 at 08:52 AM 0
Share

Hi @getyour411. The issue is occurring @ line 47.Thanks

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReference Problem with pragma strict 1 Answer

NullReferenceException when using GetComponent to access script variables 1 Answer

DLL NullReferenceException 1 Answer

NullReferenceException....Again(sorry) 0 Answers

script GetComponent, nullReference error 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges