Hi guys! Im noob here.
Im trying my game and my problem is when I try to switch between scenes, my script create 4 stances of the new scene, its so weird, I will post my code and some screenshot:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class StairsTeleport : MonoBehaviour
{
GameObject player;
void Update()
{
if (player != null)
{
StartCoroutine(LoadYourAsyncScene());
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.CompareTag("Player"))
{
player = col.gameObject;
}
}
IEnumerator LoadYourAsyncScene()
{
// Set the current Scene to be able to unload it later
Scene currentScene = SceneManager.GetActiveScene();
// The Application loads the Scene in the background at the same time as the current Scene.
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("VOID", LoadSceneMode.Additive);
// Wait until the last operation fully loads to return anything
while (!asyncLoad.isDone)
{
yield return null;
}
// Move the GameObject (you attach this in the Inspector) to the newly loaded Scene
SceneManager.MoveGameObjectToScene(player, SceneManager.GetSceneByName("VOID"));
// Unload the previous Scene
SceneManager.UnloadSceneAsync(currentScene);
}
}
I get this code from unity Unity - Scripting API: SceneManagement.SceneManager.MoveGameObjectToScene
The image with the 4 stances:
