• 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
Question by Siavash1989 · Oct 01, 2019 at 09:03 PM · dontdestroyonloadload scenecheckpoint

How to Make a Checkpoint for a 3D game!

Hi

I'm working on a 3D endless runner game and when my player falls off an edge (y < 2), my scene reloads to let the player play again. It works well but I'm trying to add a checkpoint that gets activated after a distance (Z-axis = 2400) has been run past. I'm using the script below to achieve what I want. (I've already grabbed a reference to the "RB" on this script.)

if (SceneManager.GetActiveScene().name == "Level 7") { if (RB.position.z > 2400) { if (RB.position.y < 2) { FindObjectOfType().EndGame2(); } }

Then after taking me to the method "EndGame2" on a new script named "GameManager", it continues:

public GameObject Player;

private void Awake() { Player = GameObject.Find("Player"); DontDestroyOnLoad(gameObject); }

public void EndGame2() { Invoke("Restart2", 4f); }

 public void Restart2 ()
 {
     SceneManager.LoadScene(SceneManager.GetActiveScene().name);
     Player.transform.position = new Vector 3 (0, 1, 2400);
 }

Now, the main problem is that once the level reloads, the player starts moving form still the point zero on Z-axis (not 2400) and it seems the "DontDestroyOnLoad" doesn't work well. I know I may have had many mistakes. I would appreciate it if someone please helps me with a clear explanation. Thanks in advance.

Comment

People who like this

0 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 Michael_Berna · Oct 02, 2019 at 03:34 AM 0
Share

What 3D movement script are you using? Is t something from the standard assets? In my game, the first person movement script was moving my character back to where it was previously before I loaded it. To test this theory, you can temporarily add Time.timescale = 0; before the loading code. Debug this in unity and see if the player position stays when time is paused. If it reverts, it is most likely your movement script interfering. Also, should be Vector3 rather than Vector 3

avatar image WarmedxMints · Oct 02, 2019 at 06:27 AM 1
Share

You are reloading the scene and setting the player position before it is reloading hence why your player always restarts from the default z pos.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by thomh_unity · Oct 02, 2019 at 07:25 AM

Hi Siavash198,

The Player variable you're modifying in that method is the one from the current scene. If you replace the player when the scene loads, that would be why it seems to not move. You can debug this by using the GetInstanceID(); for example (assuming that Player is a gameObject):

 public void Restart()
 {
     SceneManager.LoadScene(SceneManager.GetActiveScene().name);
     Debug.Log("What's my player's ID: " + Player.GetInstanceID());
 }

 void Start()
 {
     Debug.Log("What's my player's ID at start: " + Player.GetInstanceID());
 }



You will then see if the object you're modifying the position on is the same instance as the one you end up with after the scene load.

I hope this helps.

Comment
Michael_Berna

People who like this

1 Show 0 · Share
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

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

115 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

Related Questions

Persistent GameObject Between scene Without Serilization 1 Answer

pass data between scene for game settings? 1 Answer

URP Unity second camera (From other scene) not being detected for stacking 0 Answers

DontDestroyOnLoad reset at certain point 2 Answers

When I reload my Scene, nothing works. I think it's because of a dontDestroyOnLoad gameObject 0 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