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 TrackOdin · Feb 14, 2021 at 06:03 PM · scene-loadingtransform.positionload scenescene loadscene change

How to change transform.position after loadScene?

Hello! I'm loading a scene when pressing the button "r". The problem is that after the scene has loaded, I want to change the position of the player. It doesn't work. How should I write it? The Debug.Log("Start"); doesn't even make anything appear on the console. So the code below there doesn't run.

 private void Update()
     {
         if (Input.GetKeyDown("r"))
         {
             StartCoroutine(LoadYourAsyncScene());
         }
     }
 
     IEnumerator LoadYourAsyncScene()
     {
         AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex);
 
         while (!asyncLoad.isDone)
         {
             yield return null;
         }
         Debug.Log("Start");
         //Position changing here:
         MenthetoAdatok betoltendoAdatok = Mentes.JatekBetoltese();
         this.transform.position = new Vector3(betoltendoAdatok.karakterPozicio[0], betoltendoAdatok.karakterPozicio[1], betoltendoAdatok.karakterPozicio[2]);
         Camera.main.transform.position = new Vector3(betoltendoAdatok.kameraPozicio[0], betoltendoAdatok.kameraPozicio[1], betoltendoAdatok.kameraPozicio[2]);
     }





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
1
Best Answer

Answer by AbandonedCrypt · Feb 16, 2021 at 08:54 AM

You will want to use SceneManager.sceneLoaded delegate. This event fires every time a scene is loaded and supplies both the Scene and the Loading Mode.

 public class SceneLoadActions
 {
     [SerializeField] private Transform player; //drag player reference onto here
     private Vector3 targetPosition; //here you store the position you want to teleport your player to
     
     private void OnEnable()
     {
         SceneManager.sceneLoaded += SceneLoaded; //You add your method to the delegate
     }
     
     private void OnDisable()
     {
         SceneManager.sceneLoaded -= SceneLoaded;
     }
     
     //After adding this method to the delegate, this method will be called every time
     //that a new scene is loaded. You can then compare the scene loaded to your desired
     //scenes and do actions according to the scene loaded.
     private void SceneLoaded(Scene scene, LoadSceneMode mode)
     {
         if(scene == yourDesiredScene) //use your desired check here to compare your scene
             player.position = targetPosition;
     }
 }

Avoid solutions using FindObjectWithTag or anything, learning to work around functions like that is good to begin with.

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

Answer by RealEfrain12 · Feb 16, 2021 at 03:18 AM

First with the Debug.Log() thing, you have it in IEnumerator in which for some reason will not work, you have to put it in to a different function like Start() just not IEnumerator, and with the new position thing, you have to create an empty game object in the new scene you want to load, and type this (also make sure your player object has the tag "Player"):

 private Transform player;
 void Start()
 {
 player = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
         player.transform.position = new Vector3(0, 0, 0);
 }
 //with the 0, 0, 0 in Vector3() this means you have to put in numbers, this is the position, basically it's like this, new Vector3(2, 2, 2), or 3, 4, 1, this places your player somewhere else

also make sure you have a DontDestroyOnLoad inside your player script, it should go something like this:

 void Awake()
     {
         DontDestroyOnLoad(this.gameObject);
     }
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

161 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

Related Questions

Order of SceneManager.LoadSceneAsync and UnloadSceneAsync. 0 Answers

Unity Scene Is Loading/Not Loaded on player death 1 Answer

Start function not called after reloading scene 0 Answers

Printing total time to open/loading a scene 0 Answers

switching scenes take a long time 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