Having trouble moving a GameObject to another scene and Transform it to a different position
Hi All,
Having trouble with getting the below code to play ball. To sum it up, when interacting with a door the player should be sent to another scene and then transformed (as if they have come through the door ect). The scene changes fine, everything functions correctly except that the player game object doesn't move to the scene. Have been looking at similar posts but haven't had any luck. Any help would be appreciated.
Have cut out the unrelated code.
[CODE=CSharp]
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class PlayerInteract : MonoBehaviour {
public GameObject currentInterobj = null;
public Interactable currentInterObjScript = null;
public Inventory inventory;
public GameObject Player;
void Update()
{
Player = GameObject.Find("Player");
if (Input.GetButtonDown("Interact") && currentInterobj)
{if (currentInterObjScript.sceneLink)
{
DontDestroyOnLoad(Player);
Scene sceneToLoad = SceneManager.GetSceneByBuildIndex(currentInterObjScript.sceneNumber);
SceneManager.LoadScene(currentInterObjScript.sceneNumber);
SceneManager.MoveGameObjectToScene(Player, SceneManager.GetSceneByBuildIndex(currentInterObjScript.sceneNumber));
Player.transform.position = new Vector3(1, -4, 1);
Debug.Log("Player moved scene");
}
}
[/CODE]
I never have tried to move the player to the next scene. When I made level 2, I made it so that scene started with the player in the scene already - in your case, on the other side of the door he just walked through in scene 1. How else do you work on/debug scene 2 without a player in there? I just finish working on scene 1, then save it and strip everything out but the player and his scripts and save as scene 2, then start building scene 2. Wherever I have him at the start ( on the other side of door) is where he will be when you load scene 2.
I thought that it easier to move the player object across scenes along with all current player information... However would it be easier to save the current progress etc from scene 1 and then load this onto a separate but identical player object in my scene 2?
save current progress and delete all you don't want in scene 2. Use same player really. Thats just what I do