Can someone help me to solve my problem? After switching from a new scene, i want my player position to be near to the entry point in the previous scene where i switch to a new scene via door (BoxCollider).
Here is my sample code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SwitchingScene : MonoBehaviour
{
//designing entry and exit point for the scene switching
public string nextScene;
public string enter;
public string exit;
public void OnTriggerEnter2D(Collider2D col)
{
//or (col.CompareTag("Player")
if (col.gameObject.tag == "Player")
{
SceneManager.LoadScene(nextScene);
}
}
// designing entry and exit point for this script:
// if player position collider with the intial object(enter), it will enter a new scene.
// if the player went and collide with another object(exit),
// it will return back to its intial position(enter) from previous scene. (Unload scene?)
}
Can you help me to code this logic or show a better way to retain the player final position in the previous scene? Thank you.
Maybe a better solution than mine, which uses PlayerPrefs ;)
– KoyemsiThanks for the reply. A follow up question, can this code apply to singleton player ? If not, do i need to make duplicate player for each scene i make? (PS : is singleton and static the same thing?) Thank you.
– unity_YBW262B1IWmeXw