• 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 /
  • Help Room /
avatar image
0
Question by limitededitionskiingcroc · Mar 29, 2018 at 10:53 PM · nullreferenceexceptioncheckpointglobal variable

Checkpoints between scenes (NullReferenceException?)

Hi! I am creating my first dungeon crawler game in Unity and am in need of assistance. I want to use a checkpoint system so that after collecting an object in a scene, my game changes the player's starting position upon returning to a different scene. Ex. If the collectible from level 2 is found, when the player returns to level 1 they will start in a different position.

I tried to do t$$anonymous$$s by creating a public variable in a public global control class to hold the position between scenes, w$$anonymous$$ch can hold the position of the local checkpoint and return it when the player comes back to the checkpoint level. T$$anonymous$$s however, causes the following error when called:

NullReferenceException: Object reference not set to an instance of an object.

Below is my Checkpoint class in w$$anonymous$$ch I attempt to

 public static Vector3 checkpointPos;
 public static GameObject player;
 //Lists all gatepoints in the scene
 public static GameObject[] Checkpoints;
 //public instance of a class
 public static Checkpoint instance;

 void Awake()
 {
     //Finds all checkpoints in the current scene
     Checkpoints = GameObject.FindGameObjectsWithTag("Checkpoint");
     //Assigns player object to the player itself
     player = GameObject.FindGameObjectWithTag("Player");
     FindCheckpoint();
 }
 /*Determine w$$anonymous$$ch checkpoint to use based on # of collectibles
 **Ex. If player has not collected 2 collectibles when in gates scene,
 **it will return to the door to Level 2 until otherwise.*/
 public void FindCheckpoint()
 {
     switch (CollectibleControls.collectibles)
     {
         case 1:
             print("Called case 1");
             checkpointPos = Checkpoints[0].transform.position;
             break;
         case 2:
             print("Called case 2");
             checkpointPos = Checkpoints[1].transform.position;
             break;
         case 3:
             print("Called case 3");
             checkpointPos = Checkpoints[2].transform.position;
             break;
         case 4:
             print("Called case 4");
             checkpointPos = Checkpoints[3].transform.position;
             break;
     }
     SavePositions();
 }
 //determine if the global variable is holding a checkpoint; if not,
 //it will find one based on the number of collectibles
 public void DeterminePosition()
 {
     if (GlobalControl.checkpointPos != null)
     {
         player.transform.position = GlobalControl.checkpointPos;
     }
     else
     {
         FindCheckpoint();
     }
 }
 void SavePositions()
 {
     //Save positions to Global control to keep between scenes
     GlobalControl.checkpointPos = checkpointPos;
 }

And here is my variable in global control: public class GlobalControl : MonoBehaviour { public static Vector3 checkpointPos; } It is called only by my game manager when the player returns to the scene with the checkpoint system, w$$anonymous$$ch is referred to as gateLevel:

 public void LevelControls(Collider other)
 {
     SceneManager.LoadScene(doorLevel);
     if (doorLevel == gateLevel)
         Checkpoint.instance.DeterminePosition();
 }

Thank you so much for reading t$$anonymous$$s! I am new to the Unity community and would greatly appreciate the help.

Comment
Add comment
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

129 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException when using gameObject.SetActive(false); 0 Answers

Help needed finding NullReferenceException 1 Answer

So I know this has been asked before but, I feel this only applies to my problem. 2 Answers

NullReferenceException on a GameObject holding the main scene camera as the parent 1 Answer

Space Shooter Score Text NullReference error 3 Answers


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