How to change transform.position after loadScene?
Hello! I'm loading a scene when pressing the button "r". The problem is that after the scene has loaded, I want to change the position of the player. It doesn't work. How should I write it? The Debug.Log("Start"); doesn't even make anything appear on the console. So the code below there doesn't run.
private void Update()
{
if (Input.GetKeyDown("r"))
{
StartCoroutine(LoadYourAsyncScene());
}
}
IEnumerator LoadYourAsyncScene()
{
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex);
while (!asyncLoad.isDone)
{
yield return null;
}
Debug.Log("Start");
//Position changing here:
MenthetoAdatok betoltendoAdatok = Mentes.JatekBetoltese();
this.transform.position = new Vector3(betoltendoAdatok.karakterPozicio[0], betoltendoAdatok.karakterPozicio[1], betoltendoAdatok.karakterPozicio[2]);
Camera.main.transform.position = new Vector3(betoltendoAdatok.kameraPozicio[0], betoltendoAdatok.kameraPozicio[1], betoltendoAdatok.kameraPozicio[2]);
}
Answer by AbandonedCrypt · Feb 16, 2021 at 08:54 AM
You will want to use SceneManager.sceneLoaded delegate. This event fires every time a scene is loaded and supplies both the Scene and the Loading Mode.
public class SceneLoadActions
{
[SerializeField] private Transform player; //drag player reference onto here
private Vector3 targetPosition; //here you store the position you want to teleport your player to
private void OnEnable()
{
SceneManager.sceneLoaded += SceneLoaded; //You add your method to the delegate
}
private void OnDisable()
{
SceneManager.sceneLoaded -= SceneLoaded;
}
//After adding this method to the delegate, this method will be called every time
//that a new scene is loaded. You can then compare the scene loaded to your desired
//scenes and do actions according to the scene loaded.
private void SceneLoaded(Scene scene, LoadSceneMode mode)
{
if(scene == yourDesiredScene) //use your desired check here to compare your scene
player.position = targetPosition;
}
}
Avoid solutions using FindObjectWithTag or anything, learning to work around functions like that is good to begin with.
Answer by RealEfrain12 · Feb 16, 2021 at 03:18 AM
First with the Debug.Log() thing, you have it in IEnumerator in which for some reason will not work, you have to put it in to a different function like Start() just not IEnumerator, and with the new position thing, you have to create an empty game object in the new scene you want to load, and type this (also make sure your player object has the tag "Player"):
private Transform player;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
player.transform.position = new Vector3(0, 0, 0);
}
//with the 0, 0, 0 in Vector3() this means you have to put in numbers, this is the position, basically it's like this, new Vector3(2, 2, 2), or 3, 4, 1, this places your player somewhere else
also make sure you have a DontDestroyOnLoad inside your player script, it should go something like this:
void Awake()
{
DontDestroyOnLoad(this.gameObject);
}