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 /
  • Help Room /
avatar image
0
Question by lucassivolella · Jan 22, 2021 at 09:34 PM · 2d gamescene-loadingscene-switchingscenes

Player GameObject is not being positioned where my SceneController tells it to after scene transition.

My game has two scenes. One is called TownScene and the other CaveScene. The Player can navigate between these two after he passes through triggers, one located at the entrance of the cave and one at the exit of the Cave. I wished my Player would spawn close to the entrance/exit of the cave to get that good felling of continuity for the game, and I'm trying to do that through code, but failing miserably.

My player has one script with a Singleton, so that the same instance of the object is kept between scenes.

I have a SceneContoller script that exists in both scenes. It has two Serialized fields that hold the exact coordinantes I wish my Player would spawn. Here is the script:

 public class SceneController : MonoBehaviour
 {
     [SerializeField] Vector2 townSpawnPoint;
     [SerializeField] Vector2 caveSpawnPoint;
     [SerializeField] GameObject playerGameObject;
 
     //private GameObject playerGameObject;
     private string activeSceneName;
 
     private void OnEnable()
     {
         SceneManager.sceneLoaded += OnSceneLoaded;
     }
 
     private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
     {
         GetActiveScene();
         if (activeSceneName == "TownScene")
         {
            playerGameObject.GetComponent<Rigidbody2D>().transform.position = townSpawnPoint;
         }
         if (activeSceneName == "CaveScene")
         {
             playerGameObject.GetComponent<Rigidbody2D>().transform.position = caveSpawnPoint;
         }
     }
 
     private void OnDisable()
     {
         SceneManager.sceneLoaded -= OnSceneLoaded;
     }
 
     private void GetActiveScene()
     {
         activeSceneName = SceneManager.GetActiveScene().name;
     }
 
     public void LoadCaveScene()
     {
         SceneManager.LoadScene("CaveScene");
     }
 
     public void LoadTownScene()
     {
         SceneManager.LoadScene("TownScene");
     }
 }


You can see that the OnSceneLoaded method will pass the spawn coordinates to the Player's Rigidbody right when the scene is enabled. That works fine, but just once, when I first play the game. That means that starting the game in the TownScene will make the Player spawn at the townScenePoint, and that starting the game in the CaveScene will make the Player spawn at the caveScenePoint. But after that, if I go through the triggers, the Scene will switch and the Player will keep the coordinates from the previous scene.

I cannot figure out how to fix this. I did some Logs to see if these methods were being executed and they are. So how can I make it run consistently? I would appreciate some help, I have been trying to fix it for a couple of days with no luck.

Another issue going on is that, in the very begging of the game, I wished my Player would spawn at the town entrance, but as I said above, the SceneController script will make him spawn at the townSpawnPoint (that's at the cave entrance). I could not get around that either, but I would be glad already if I could fix the first issue. Sorry for the long text, but I tried to be very clear about what is going on.

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

1 Reply

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

Answer by lucassivolella · Jan 26, 2021 at 05:39 PM

I manage to fix the first issue after a while. I was so focused on getting it right using the SceneController script that it blinded me to alternatives. As to why it did not work before, the playerGameObject from one scene seems to be a different instance from the other scene, even though I had a DontDestroyOnLoad for it. The way I fixed it was placing the method that sets the correct spawn location at the player script. The SceneController just calls for it OnSceneLoaded.

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

Follow this Question

Answers Answers and Comments

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

Is there a way to have an Awake() run every time a scene is loaded. 0 Answers

What is the best for performance? 0 Answers

When I load a scene a second time, some objects don't show up 0 Answers

Multiple Entrances to levels 0 Answers

Game freezes after button is clicked to switch scenes 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