Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Moving Character to another scene

Discussion in 'Scripting' started by Azux, Sep 17, 2020.

  1. Azux

    Azux

    Joined:
    Mar 3, 2015
    Posts:
    19
    Hi i have trouble with moving my camera and player to another scene. My player has stats and for me best way is to move him instead of instantinating him once again and then loading his stats. But now in my other scene (im using addiative mode) i have to set him up in new spawnpoint position but for some reason script cannot find a new spawn point, it is using same coordinates for spawn point from scene before.

    Code (CSharp):
    1.    
    2.     public GameObject player_prefab;
    3.     public Transform player_SpawnPoint;
    4.     public static GameObject player_instance;
    5.  
    6.     private void Awake()
    7.     {
    8.         DontDestroyOnLoad(this.gameObject);
    9.  
    10.         SceneManager.sceneLoaded += OnSceneLoaded;
    11.     }
    12.  
    13.     void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    14.     {
    15.         Debug.Log("OnSceneLoaded: " + scene.name);
    16.         player_SpawnPoint = GameObject.FindGameObjectWithTag("Player_SpawnPoint").transform;
    17.  
    18.         if (player_instance == null)
    19.         {
    20.             GameObject player = Instantiate(player_prefab, player_SpawnPoint.position, Quaternion.identity) as GameObject;
    21.             player_instance = player;
    22.             Camera.main.GetComponent<CameraSC>().followTarget = player.transform;
    23.         }
    24.         Debug.Log(player_SpawnPoint.position);
    25.  
    26.     }
    27.  
    So here im trying to find an object with tag of spawn point after scene is loaded, so it works good on scene 1 but when i go to scene two spawn point remain the same. But when i change mode from addiative to single it works good just i have few objects to move and only one is moved.
     
  2. Azux

    Azux

    Joined:
    Mar 3, 2015
    Posts:
    19
    OK so i think i solved the problem this way
    Code (CSharp):
    1.     public GameObject player_prefab;
    2.     public Transform player_SpawnPoint;
    3.     public static GameObject player_instance;
    4.     public string sceneID;
    5.  
    6.  
    7.     bool isPlayerSpawned = false;
    8.     private void Awake()
    9.     {
    10.         DontDestroyOnLoad(this.gameObject);
    11.         player_SpawnPoint = GameObject.Find("Player_SpawnPoint" + "_" + SceneManager.GetActiveScene().name).transform;
    12.         Debug.Log(player_SpawnPoint.position);
    13.  
    14.         if (player_instance == null)
    15.         {
    16.             GameObject player = Instantiate(player_prefab, player_SpawnPoint.position, Quaternion.identity) as GameObject;
    17.             player_instance = player;
    18.             Camera.main.GetComponent<CameraSC>().followTarget = player.transform;
    19.             isPlayerSpawned = true;
    20.         }
    21.  
    22.     }
    23.  
    24.     private void Update()
    25.     {
    26.         if(player_SpawnPoint == null && isPlayerSpawned)
    27.         {
    28.             isPlayerSpawned = false;
    29.             player_SpawnPoint = GameObject.Find("Player_SpawnPoint" + "_" + SceneManager.GetActiveScene().name).transform;
    30.             player_instance.transform.position = player_SpawnPoint.position;
    31.             isPlayerSpawned = true;
    32.         }
    33.     }