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.

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");
                    }
                }

Here is a modified version of the script that should fix the problem of the player game object not moving to the scene: `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 Start()
{
    Player = GameObject.Find("Player");
}

void Update()
{
    if (Input.GetButtonDown("Interact") && currentInterobj)
    {
        if (currentInterObjScript.sceneLink)
        {
            DontDestroyOnLoad(Player);
            SceneManager.LoadScene(currentInterObjScript.sceneNumber);
            StartCoroutine(MovePlayerToScene());
        }
    }
}

IEnumerator MovePlayerToScene()
{
    yield return new WaitForSeconds(0.1f);
    SceneManager.MoveGameObjectToScene(Player, SceneManager.GetSceneByBuildIndex(currentInterObjScript.sceneNumber));
    Player.transform.position = new Vector3(1, -4, 1);
    Debug.Log("Player moved scene");
}

}
`

Explanation:

I added a start method and initialized the player game object in that method, so it always finds the player object in the scene.
I move the MoveGameObjectToScene method inside a coroutine and added a small delay before executing it. This is because the scene is not fully loaded when the method is called, so the player object is not found in the new scene.
Also I added a debug log to check if player moved to new scene or not