• 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
1
Question by Shnuckles · Dec 13, 2018 at 06:23 AM · scenescene-switchingscene loadscene change

Connect multiple scenes to one main scene

So I'm making a 2d RPG (not to sell because I'm still pretty new with coding and just trying to learn) and my plan is to have multiple scenes (which will act as NPC houses) and allow the player to explore each house.


But my problem comes from trying to return to the main scene, the world_scene. I want the player too, when exiting one of the house scenes, to appear outside that house in the main world_scene. But he just pops back to whatever position he was in when I created the main world_scene. I know this is because I'm unloading the world_scene, than reloading the same way it was when I first created it.


Throwing a DontDestroyOnLoad on my player leads to the player spawning at (0, 0, 0) in whatever scene he switches to.


So my question is, how can create a way for my player to travel to any NPC house (which will load a new scene) and have him then reload the main_world scene at the same position he was at when he first loaded the new scene.


Been stuck on this for a while, any help will be much appreciated!

Comment
Add comment · Show 1
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 hexagonius · Dec 13, 2018 at 06:39 AM 0
Share

persistently save the position before loading a house. use it, if available, ins$$anonymous$$d of the default spawn location. PlayerPrefs can be of help, a static spawn position variable...

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by pruthv · Dec 13, 2018 at 06:46 AM

Try storing player's position using PlayerPrefs just before you exit the World scene. Now, when your player come back to World scene from other scene, read and set player's position using PlayerPrefs.

Just make sure you also have an If statement running if there is no value stored in PlayerPrefs, which will put player into the initial position of World scene.

I don't know how you're triggering the scene change, if you can provide some code than I can help you with hard coding this.

Comment
Add comment · Show 4 · 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 Shnuckles · Dec 13, 2018 at 06:55 AM 0
Share
 public class PlayerLoadNewScene : $$anonymous$$onoBehaviour
 {
     [SerializeField] private string sceneToLoad;
 
     private bool canLoadScene;
 
     private void Update()
     {
         LoadNewScene();
     }
 
     private void LoadNewScene()
     {
         if (canLoadScene == true && Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space))
         {
             Scene$$anonymous$$anager.LoadSceneAsync(sceneToLoad);
         }
     } 
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         canLoadScene = true;
     }
 }

I stripped down to the basics after a few failed attempts at implementing this feature. But the idea is that, when a player enters a box collider 2D, they will be able to press 'space' and then load that scene. I also Serialized a string which will allow me to type what scene I want to load in the inspector. I thought this could help with prefabing my load script on an empty gameobject to allow for easy setup when I want to add a new scene that the player can load.

Thanks for the quick reply!

avatar image pruthv Shnuckles · Dec 13, 2018 at 07:32 AM 0
Share

@Shnuckles There you go... Unity doesn't allow to store Vector2 in PlayerPrefs so I'm using individual x and y float values to store the position as you said it is 2D game. Let me know if you encounter any issues

     public class PlayerLoadNewScene : $$anonymous$$onoBehaviour
      {
          [SerializeField] private string sceneToLoad;
      
          private bool canLoadScene;
     
          public GameObject player;  // your Player or NPC whose position you want to set
          private float xPosition;   // Storing last known x and y position in this
          private float yPosition;
          private Vector2 playerPos;     // used for combining xPosition and yPosition into Vector2, just safe practice
         private float startXpos;    // Initial position when you load the World scene for the first time
          private float startYpos;
          
          private void Start()
          {
              xPosition = PlayerPrefs.GetFloat("X Position", startXpos);  // Gets xPosition if it stored, else will take startXPos for default
              yPosition = PlayerPrefs.GetFloat("Y Position", startYpos);
             playerPos = new Vector2(xPosition, yPosition);  // combining x and y into Vector2
             player.transform.position = playerPos;  // assigning player position
     
          }
          private void Update()
          {
              LoadNewScene();
          }
      
          private void LoadNewScene()
          {
              if (canLoadScene == true && Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space))
              {
                  Scene$$anonymous$$anager.LoadSceneAsync(sceneToLoad);
              }
          } 
      
          private void OnTriggerEnter2D(Collider2D collision)
          {
             xPosition = player.transform.position.x;    // using same variable to store the new position before exiting the scene,
             yPosition = player.transform.position.y;    // however if you get issues than I would suggest you to create new variable for this.
             PlayerPrefs.SetFloat("X Position", xPosition);  // storing the player position values
             PlayerPrefs.SetFloat("Y Position", yPosition);
     
             canLoadScene = true;
          }
      }
avatar image Shnuckles pruthv · Dec 13, 2018 at 07:53 AM 1
Share

@pruthv Thanks! The only thing I changed was that I put the extra functionality that you put in the OnTriggerEnter2D into the LoadNewScene if statement. So now when I press space it will save my position.

Thanks for your help!

Show more comments

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

116 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

Related Questions

SceneManager.LoadScene not working with button 1 Answer

Help with error message "Overwriting the same path as another open scene is not allowed" 0 Answers

,Enums not being passed between scripts properly 1 Answer

Load Scene from .unity File 1 Answer

What is happening to scene manager.loadscene??? 2 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