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
1
Question by sk8terboy4 · Mar 07, 2016 at 11:16 AM · scripting problemlightingscene-loadingasyncscene load

Use of SceneManager.MoveGameObjectToScene?

Hi,

I am trying to move my player/gameobject to the next scene being loaded.

  public void loadLevelasync(int id, GameObject player)
     {
         StartCoroutine(fadeIn(loadingCanvas, 2.0f));
 
         Scene nextLevel = SceneManager.GetSceneAt(id);
         SceneManager.MoveGameObjectToScene(player, nextLevel);
         StartCoroutine(loadScene(id));
     }
   
     IEnumerator loadScene(int id)
     {
         AsyncOperation async = SceneManager.LoadSceneAsync(id);
         while (!async.isDone)
         {
             loadingBar.value = async.progress / 0.9f;
             yield return null;
         }
     }

I can load the scene async perfectly, but my gameobject is not in the next loaded scene hierarchy and the lighting is not fully done.

How exactly does MoveGameObjectToScene work?

Thanks, Austin

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 meat5000 ♦ · Mar 07, 2016 at 11:15 AM 0
Share

I believe the move must be completed after the load.

avatar image sk8terboy4 meat5000 ♦ · Mar 07, 2016 at 10:27 PM 1
Share

That didn't work. I debug the code and it seems that Scene$$anonymous$$anager.GetSceneAt(id) is returning the current level. So say if I am on level1 with ID of 0 and level2 has ID of 1. The nextLevel variable has the scene "level1" with ID of 0 after doing Scene$$anonymous$$anager.GetSceneAt(1). So It's not moving the gameobject, but the loadScene coroutine is working and loads level2?

Is this a bug?

 public void loadLevelasync(int id, GameObject player)
     {
         StartCoroutine(fadeIn(loadingCanvas, 2.0f));
        
         StartCoroutine(loadScene(id));
         
         Scene nextLevel = Scene$$anonymous$$anager.GetSceneAt(id);
         Debug.Log("Player scene: " + player.scene.name + " Next Scene: " + nextLevel.name);
         Scene$$anonymous$$anager.$$anonymous$$oveGameObjectToScene(player, nextLevel);
         Debug.Log("Player scene: " + player.scene.name + " Next Scene: " + nextLevel.name);
         
 
 
     }
  
     IEnumerator loadScene(int id)
     {
         AsyncOperation async = Scene$$anonymous$$anager.LoadSceneAsync(id);
         while (!async.isDone)
         {
             loadingBar.value = async.progress / 0.9f;
             yield return null;
         }
 
     }

Edit: Did some more debugging and I found out that GetSceenAt() is using the indexes that GetAllScenes() uses. The first image in the attachment is the build settings.

  public void loadLevelasync(int id, GameObject player)
     {
         StartCoroutine(fadeIn(loadingCanvas, 2.0f));
        
         StartCoroutine(loadScene(id));
         
         Scene nextLevel = Scene$$anonymous$$anager.GetSceneAt(Scene$$anonymous$$anager.GetActiveScene().buildIndex-1);
         Debug.Log("Player scene: " + player.scene.name + " Next Scene: " + nextLevel.name);
         Scene$$anonymous$$anager.$$anonymous$$oveGameObjectToScene(player, nextLevel);
         Debug.Log("Player scene: " + player.scene.name + " Next Scene: " + nextLevel.name);
         Debug.Log("Scene Count: " + Scene$$anonymous$$anager.sceneCount + " : Scene Count in Build Settings: " + Scene$$anonymous$$anager.sceneCountInBuildSettings);
         showSceneNames();
 
     }
     public void showSceneNames()
     {
         for(int i = 0; i < Scene$$anonymous$$anager.GetAllScenes().Length; i++)
         {
             Debug.Log(i + " : " + Scene$$anonymous$$anager.GetAllScenes()[i].name + " " + Scene$$anonymous$$anager.GetAllScenes()[i].buildIndex);
         }
     }

The second image is the debug output after the next level is loaded.

As you can see from the debug output, i cannot use the indexes used for the build settings. I still cannot move the gameobject though. The gameobject is not in the next scene when the next level is loaded [1]: /storage/temp/65436-drrso.jpg [2]: /storage/temp/65437-khqme.jpg

khqme.jpg (129.0 kB)
drrso.jpg (164.5 kB)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Zackton · Aug 16, 2016 at 06:47 AM

@sk8terboy4 Instead of using SceneManager.MoveGameObjectToScene(player, nextLevel), you can simply use:

 Object.DontDestroyOnLoad(player.gameObject); //You might be able to just use 'player'.

Or if the script you are on is the player's script, you can simply use:

 Object.DontDestroyOnLoad(this.gameObject); //You might be able to just use 'this'

Sorry about the long time waiting, I have only come across this problem today.

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 RomanLed · Oct 07, 2016 at 09:24 PM

This is how to keep objects (different than moving objects from scene to scene however it might help you)... https://www.youtube.com/watch?v=S9h_iu4Zx5I&feature=youtu.be

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

SceneManager.sceneLoaded returns scene.isLoaded as false, is this intended? 1 Answer

Some scripts seem to be breaking when re-loading a scene 0 Answers

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

How I can Open a Previous Scene with button back !? 3 Answers

How to change transform.position after 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