Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by bassinastor · Feb 13, 2018 at 11:30 PM · player movementscene-switching

How to maintain player position between scenes?

For example, in Scene1 the player is located at (1,2,3) then the scene is changed to Scene2 and the player should be located at (1,2,3) in the new scene.

I figure I'll need to get the player position first, then change scene, and then translate the player to that position, but I don't know how I would store that position between scenes.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by LiamofElites · Feb 14, 2018 at 01:11 AM

The best solution for this is to use DontDestroyOnLoad. This will keep the gameobject when transitioning to another scene.


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class dontDestroy : MonoBehaviour {
 
     int scene = 1;
 
     void Update() {
         if (Input.GetKeyDown(KeyCode.Space)) {
             if (scene == 1) {
                 SceneManager.LoadScene("Scene1");
                 DontDestroyOnLoad(transform.gameObject);
                 scene++;
             } else {
                 SceneManager.LoadScene("Scene");
                 scene--;
             }
         }
     }
 }


Comment
Add comment · Show 3 · 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
avatar image bassinastor · Feb 14, 2018 at 01:45 AM 0
Share

Thank you, this works perfectly! I had found other threads suggesting the DontDestroyOnLoad method, but I couldn't figure out how to implement it. One problem I have with this is that when I load the original scene it spawns another "player". I assume I should add some check for other player objects in a scene, do you know the best way to go about that?

avatar image bassinastor bassinastor · Feb 14, 2018 at 02:36 AM 0
Share

I ended up using "FindGameObjectsWithTag" to find all Players, and then deleting all of them past the first. Is this a bad solution?

avatar image LiamofElites bassinastor · Feb 14, 2018 at 02:38 AM 0
Share

Sorry for the confusion, I set DontDestroyOnLoad to the same gameobject every time the scene was changed. This in turn would duplicated the gameobject from the last scene, I've updated my answer's code to

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Scene$$anonymous$$anagement;
 
 public class dontDestroy : $$anonymous$$onoBehaviour {
 
     int scene = 1;
 
     void Update() {
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) {
             if (scene == 1) {
                 DontDestroyOnLoad(transform.gameObject);
                 Scene$$anonymous$$anager.LoadScene("Scene1");
                 scene++;
             } else {
                 Scene$$anonymous$$anager.LoadScene("Scene");
                 scene--;
             }
         }
     }
 }
avatar image
0

Answer by Betoso · Feb 14, 2018 at 12:17 AM

Hello i will give you an easy solution but i don´t know if it is the best solution.

in the Start method in someone class in the player´s prefab write this lines of code.

void Start() { transform.position=new vector3(x,y,z); }

(x,y,z) are starting point. I don´t know if it is usefull for you. If you need more about it ask and i will help you if is possible.

Cheers

Comment
Add comment · Show 1 · 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
avatar image Betoso · Feb 14, 2018 at 12:39 AM 0
Share

If the new position in the scene is changeible the way should be different. You can to get the position as you said and use don´t destroyOnLoadScene, them you translate your player to that position in the Start method. If you need more information ask.

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

76 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

Related Questions

2D player sliding after scene transition 0 Answers

Accessing a script from another script in different scene 1 Answer

Loading and Unloading scenes 2 Answers

I'm facing issue in creating a MainMenu of a 2D Game in Unity 5.5 0 Answers

How do I keep a characters position in a scene change? 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