- Home /
Scene manager Move object to scene creating too many items.,scene manager move object to scene creating too many items
When i am transfering scenes it just keeps creating more and more of the player block how do i fix this code using UnityEngine; using System.Collections; using UnityEngine.SceneManagement;
[RequireComponent(typeof(Player))] public class PlayerInput : MonoBehaviour {
Player player;
// Use this for initialization
void Start()
{
player = GetComponent<Player>();
}
// Update is called once per frame
void Update()
{
Vector2 directionalInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
player.SetDirectionalInput(directionalInput);
if (Input.GetKeyDown(KeyCode.UpArrow))
{
player.OnJumpInputDown();
}
if (Input.GetKeyUp(KeyCode.UpArrow))
{
player.OnJumpInputUp();
}
if (Input.GetKeyDown(KeyCode.X))
{
StartCoroutine(LoadYourAsyncScene());
}
if (Input.GetKeyDown(KeyCode.Z))
{
SceneManager.LoadScene("Present", LoadSceneMode.Additive);
}
}
IEnumerator LoadYourAsyncScene()
{
Scene currentScene = SceneManager.GetActiveScene();
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("Future", LoadSceneMode.Additive);
while (!asyncLoad.isDone)
{
yield return null;
}
SceneManager.MoveGameObjectToScene(gameObject, SceneManager.GetSceneByName("Future"));
SceneManager.UnloadSceneAsync(currentScene);
}
} ,when i am changing scenes my game just creats more and more players and it is not removing the old ones. This is my code. using UnityEngine; using System.Collections; using UnityEngine.SceneManagement;
[RequireComponent(typeof(Player))] public class PlayerInput : MonoBehaviour {
Player player;
// Use this for initialization
void Start()
{
player = GetComponent<Player>();
}
// Update is called once per frame
void Update()
{
Vector2 directionalInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
player.SetDirectionalInput(directionalInput);
if (Input.GetKeyDown(KeyCode.UpArrow))
{
player.OnJumpInputDown();
}
if (Input.GetKeyUp(KeyCode.UpArrow))
{
player.OnJumpInputUp();
}
if (Input.GetKeyDown(KeyCode.X))
{
StartCoroutine(LoadYourAsyncScene());
}
if (Input.GetKeyDown(KeyCode.Z))
{
SceneManager.LoadScene("Present", LoadSceneMode.Additive);
}
}
IEnumerator LoadYourAsyncScene()
{
Scene currentScene = SceneManager.GetActiveScene();
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("Future", LoadSceneMode.Additive);
while (!asyncLoad.isDone)
{
yield return null;
}
SceneManager.MoveGameObjectToScene(gameObject, SceneManager.GetSceneByName("Future"));
SceneManager.UnloadSceneAsync(currentScene);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613050012im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Function triggered when scene changes? 4 Answers
UI canvas gets copied when changing scene 2 Answers
Switching Scene Immediately 2 Answers
Why won't invoke work in my script to delay the scene change? 1 Answer
Scene Loading Issue. 0 Answers