Search Unity

Some Questions About the "MoveGameObjectToScene" Command

Discussion in '2D' started by Th0masThe0bvious, Nov 19, 2018.

  1. Th0masThe0bvious

    Th0masThe0bvious

    Joined:
    Aug 22, 2018
    Posts:
    80
    This is obviously quite an important function for many games. Sadly, I have been having some trouble getting it to work. It seems I'm not the first person who has had such trouble, and apparently there are probably other commands that need to be implemented first in order for it to work. I prefer to keep code simple while I'm learning, so let's look at this from a minimum possible fix angle. Here are my two questions:

    1) Why doesn't this code work?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class Warp : MonoBehaviour {
    7.  
    8.     public GameObject Traveler;
    9.     public string Destination;
    10.  
    11.     private void OnTriggerEnter2D(Collider2D other)
    12.     {
    13.         if(other.gameObject.tag == "Player")
    14.         {
    15.             SceneManager.LoadScene(Destination);
    16.             SceneManager.MoveGameObjectToScene(Traveler, SceneManager.GetSceneByName(Destination));
    17.         }
    18.     }
    19. }
    20.  
    It doesn't crash the game and it successfully loads the next scene, but the "Traveler" object isn't transferred. (Note: It's the player object, dragged into that entry box in Inspector?) What all do I need to add to make the game object move successful?

    2) Is there a way to set the Traveler objects starting position within the new scene? I'm going to assume there is, but I haven't found much online as to how.
     
    BBG1468 likes this.
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    I have nevere heard about this :) In the manual is written: This only works on GameObjects being moved to a Scene that is already loaded (additive).
    And as example,
    Code (CSharp):
    1. IEnumerator LoadYourAsyncScene()
    2.     {
    3.         // Set the current Scene to be able to unload it later
    4.         Scene currentScene = SceneManager.GetActiveScene();
    5.  
    6.         // The Application loads the Scene in the background at the same time as the current Scene.
    7.         AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(m_Scene, LoadSceneMode.Additive);
    8.  
    9.         // Wait until the last operation fully loads to return anything
    10.         while (!asyncLoad.isDone)
    11.         {
    12.             yield return null;
    13.         }
    14.  
    15.         // Move the GameObject (you attach this in the Inspector) to the newly loaded Scene
    16.         SceneManager.MoveGameObjectToScene(m_MyGameObject, SceneManager.GetSceneByName(m_Scene));
    17.         // Unload the previous Scene
    18.         SceneManager.UnloadSceneAsync(currentScene);
    19.     }
    I think, you are just switching the scene.
     
  3. Th0masThe0bvious

    Th0masThe0bvious

    Joined:
    Aug 22, 2018
    Posts:
    80
    Should I copy and paste that, or just put certain aspects in my current code? Where in the code should they go?
     
  4. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    I have just copypasted from: :D https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.MoveGameObjectToScene.html

    maybe it can be:
    Code (CSharp):
    1.     public GameObject Traveler;
    2.     public string Destination;
    3.  
    4.     private void OnTriggerEnter2D(Collider2D other)
    5.     {
    6.         if (other.gameObject.tag == "Player")
    7.         {
    8. //perhaps your should disable next trigger checks
    9.             StartCoroutine(LoadYourAsyncScene());
    10.             //SceneManager.LoadScene(Destination);
    11.             //SceneManager.MoveGameObjectToScene(Traveler, SceneManager.GetSceneByName(Destination));
    12.         }
    13.     }
    14.  
    15.     IEnumerator LoadYourAsyncScene()
    16.     {
    17.         // Set the current Scene to be able to unload it later
    18.         Scene currentScene = SceneManager.GetActiveScene();
    19.  
    20.         // The Application loads the Scene in the background at the same time as the current Scene.
    21.         AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(Destination, LoadSceneMode.Additive);
    22.  
    23.         // Wait until the last operation fully loads to return anything
    24.         while (!asyncLoad.isDone)
    25.         {
    26.             yield return null;
    27.         }
    28.  
    29.         // Move the GameObject (you attach this in the Inspector) to the newly loaded Scene
    30.         SceneManager.MoveGameObjectToScene(Traveler, SceneManager.GetSceneByName(Destination));
    31.         // Unload the previous Scene
    32.         SceneManager.UnloadSceneAsync(currentScene);
    33.     }
     
  5. Th0masThe0bvious

    Th0masThe0bvious

    Joined:
    Aug 22, 2018
    Posts:
    80
    Edit: That sort of works, but I still need to get my player object to some convenient coordinates once he transfers. Any idea how?
     
  6. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    sorry, I don't know how this MoveGameObjectToScene works.
    maybe you can try to change position befor the moving.
    Code (CSharp):
    1. Traveler.transform.position = new Vector3 (x, y, z);
    2. SceneManager.MoveGameObjectToScene(Traveler, SceneManager.GetSceneByName(Destination));
    or
    Code (CSharp):
    1. void Start () {
    2. if (neededScene) {
    3. transform.position = neededPosition;
    4. }
    5. }
     
  7. Th0masThe0bvious

    Th0masThe0bvious

    Joined:
    Aug 22, 2018
    Posts:
    80
    I actually was able to do this after the scene transfer, in the same command, using a destination object in the new scene. Thanks, and sorry for the late reply.
     
  8. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    It might be easier to use DontDestroyOnLoad, so that you don't have to load the scene additive.
     
  9. Khilat07

    Khilat07

    Joined:
    Jan 2, 2019
    Posts:
    3
    how to use that will you please elaborate?
     
  10. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Dont destroy on load works like this. You do something like DontDestroyOnLoad(Player);, which will mean that when you load a new scene, that object you put in the () won't be destroyed.
     
    BBG1468 and Khilat07 like this.
  11. Khilat07

    Khilat07

    Joined:
    Jan 2, 2019
    Posts:
    3
    thank you ! :)