Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 rateusz · Jan 20 at 06:04 PM · collisionscript.transform.positionsingletonteleporting

Field value changes after SceneManager.LoadScene been called

Hi, I'm still pretty new to Unity.

After my player changes from one to scene to another I wanted him to change its position if the two scenes are 1 and 2. In the Debug.Log(player.position) it shows that the position does change but the player is still in the center of the screen. If I have Debug.Log(houseExit) which is a bool to see if the player has left from the house, it changes to true for a single frame and then back to false, although not specified to do so in the if statement.

 public class SceneManager : MonoBehaviour
 {
     public static SceneManager Instance { get; set; }
     public int scene;
     public int currentScene;
     Transform player;
     public Vector3 afterHouse = new Vector3(-13.4f, -0.6f, 3.8f);
     bool houseExit = false;
 
     void Awake ()
     {
         player = GameObject.Find("Player").GetComponent<Transform>();
         if (Instance != null && Instance != this)
         {
             Destroy(gameObject);
         }
         else
         {
             Instance = this;
         }
     }
     void Update ()
     {
         if (houseExit == true)
         {
             Debug.Log("Works!");
             Debug.Log(player.transform.position);
             player.transform.position = afterHouse;
             Debug.Log(player.transform.position);
         }
     }
     public void Position(int Scene, int CurrentScene)
     {
         scene = Scene;
         currentScene = CurrentScene;
 
         if (scene == 1 && currentScene == 2)
         {
             player.position = afterHouse;
             houseExit = true;
         }        
     }
 }

This is the other script that handles the collision:

 public class SceneSwitch : MonoBehaviour
 {
     public Transform player;
     public int scene;
     public int currentScene;
     void OnTriggerEnter ( Collider other )
     {
         if( other.gameObject.tag == "Player" )
         {
             SceneManager.LoadScene( scene );
             SceneManager.Instance.Position( scene , currentScene );
         }
     }
 }


Comment
Add comment · 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 andrew-lukasik · Jan 20 at 06:18 PM 0
Share


  • don't name your component SceneManager as it will conflict with existing UnityEngine.SceneManagement.SceneManager sooner or later

  • calling SceneManager.LoadScene() will remove all existing GameObjects and load the new scene


If the new scene contains instance of this your SceneManager script as well as the old one - you might be under the impression that your script state survived the scene change, but it did not. The old instance of SceneManager was deleted (along with it's state of houseExit field) and a new SceneManager was created in the new scene, with it's default value i.e. false ( bool houseExit = false;).

To fix this you need to learn about a way to make some game objects persist between scene changes. For example, say hi to your new friend DontDestroyOnLoad.


Pozdr.

avatar image rateusz andrew-lukasik · Jan 20 at 06:22 PM 0
Share

Thank you for your help! I'll try using DontDestroyOnLoad. I already tried it once on my player script but I guess I must've used it wrong. But still thank you for the info :)

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.

Follow this Question

Answers Answers and Comments

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

Code is acting different depending on call 2 Answers

Why isn't my C# code sending updates to my text scoreboard? 1 Answer

Collider's codes works, while dont colliding.. 1 Answer

How do you make the character stuck on the ceilling when at that moment the velocity.y is > 0 0 Answers

Teleportation with gun 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