Hi folks,
if I passing the portal, the scene will loading multiple times until it load the right scene, and without stuff in the inventory.
If I run through the portal trigger and don`t stop in the trigger range, after the transition i am back in the scene before, but the saving file has the save data from the other scene.
If I use the portals without saving it work fine.
I checked that I have only once the Fader, SavingWrapper und Savingsystem. And the link is correct.
I use a gamepad direct movement, thats why I don’t need the navmeshAgent. I tried out with the NavmeshAgent but there is the same issue.
Here the Portal code:
public class Portal : MonoBehaviour
{
enum DestinationIdentifier
{
A, B, C, D, BurgSchauenberg, BurgFideliegg, HausAlteFrau
}
[SerializeField] string sceneToLoad = null;
[SerializeField] Transform spawnPoint;
[SerializeField] DestinationIdentifier destination;
[SerializeField] float fadeOutTime = 3f;
[SerializeField] float fadeInTime = 3f;
[SerializeField] float fadeWaitTime = 0.5f;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
print("Player has entered portal " + gameObject.name + gameObject.GetInstanceID());
StartCoroutine(Transition());
}
}
private IEnumerator Transition()
{
if (sceneToLoad == null)
{
Debug.Log("Scene to load not set!");
yield break;
}
// We take the portal object into the next scene
DontDestroyOnLoad(gameObject);
// We fade out the Scene
Fader fader = FindObjectOfType<Fader>();
yield return fader.FadeOut(fadeOutTime);
// We save the current level
SavingWrapper savingWrapper = FindObjectOfType<SavingWrapper>();
savingWrapper.Save();
yield return SceneManager.LoadSceneAsync(sceneToLoad);
// Load current level
savingWrapper.Load();
Portal otherPortal = GetOtherPortal();
UpdatePlayer(otherPortal);
savingWrapper.Save();
// Time between the Scenes
yield return new WaitForSeconds(fadeWaitTime);
// fade in
yield return fader.FadeIn(fadeInTime);
Destroy(gameObject);
}
private void UpdatePlayer(Portal otherPortal)
{
GameObject player = GameObject.FindWithTag("Player");
player.GetComponent<CharacterController>().enabled = false;
player.transform.position = otherPortal.spawnPoint.position;
player.transform.rotation = otherPortal.spawnPoint.rotation;
player.GetComponent<CharacterController>().enabled = true;
}
private Portal GetOtherPortal()
{
foreach (Portal portal in FindObjectsOfType<Portal>())
{
if (portal == this) continue;
if (portal.destination != destination) continue;
return portal;
}
return null;
}
Thanks in advanced