Data doesn't load at start of scene after entering portal

Hi
Inside my scene I have no problem with “S” I can save and with “L” I can load, also weapon load with no problem. but after weapon save lecture whole my transition data and load it between scenes doesn’t work any more also stats doesn’t load I have to press “L” to load them again . I checked the player identifier and it was ok (“player”). but no health no weapon nothing from previous scene come with Persistent Object any more. If I hit “L” in second scene it does load all data but change my player position to some where else (“Looks like previous scene position again”)

Thanks for your help

Are there any error messages in the console window when you move between scenes?

Paste in your Portal.cs and we’ll take a look. Be sure to type three backwards apostrophes (the one next to the 1 on your keyboard) before pasting, and at the end of the paste. This will format the code for easy readability.
Example:
```
void DoSomething()
{
//Do Something;
}
```
becomes

void DoSomething()
{
     //Do Something;
}
1 Like

Thanks for reply.
I got no error message in console .
This is my Portal.cs

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.AI;
using RPG.Control;

namespace RPG.SceneManagement
{
    public class Portal : MonoBehaviour
    {
        enum DestinationIdentifier
        {
            A, B, C, D, E
        }

        [SerializeField] int sceneToLoad = -1;
        [SerializeField] Transform spwanPoint;
        [SerializeField] DestinationIdentifier destination;
        [SerializeField] float fadeOutTime = 1f;
        [SerializeField] float fadeInTime = 2f;
        [SerializeField] float fadeWaitTime = 0.5f;
        private void OnTriggerEnter(Collider other)
        {
            if (other.tag == "Player")
            {

                StartCoroutine(Transition());
            }

        }

        private IEnumerator Transition()
        {
            if (sceneToLoad < 0)
            {
                Debug.LogError("Scene to load not set.");
                yield break;
            }
            DontDestroyOnLoad(gameObject);

            Fader fader = FindObjectOfType<Fader>();
            yield return fader.FadeOut(fadeOutTime);

            // Svae Current Level
            SavingWrapper savingWrapper = FindObjectOfType<SavingWrapper>();

            yield return fader.FadeOut(fadeOutTime);


            savingWrapper.Save();


            yield return SceneManager.LoadSceneAsync(sceneToLoad);
            PlayerController newPlayerController = GameObject.FindWithTag("Player").GetComponent<PlayerController>();
            newPlayerController.enabled = false;

            savingWrapper.Load();

            Portal otherPortal = GetOtherPortal();
            UpdatePlayer(otherPortal);

            savingWrapper.Save();

            yield return new WaitForSeconds(fadeWaitTime);
            yield return fader.FadeIn(fadeInTime);
            Destroy(gameObject);

        }

        private void UpdatePlayer(Portal otherPortal)
        {
            GameObject player = GameObject.FindWithTag("Player");
            player.GetComponent<NavMeshAgent>().enabled = false;
            player.transform.position = otherPortal.spwanPoint.position;
            player.transform.rotation = otherPortal.spwanPoint.rotation;
            player.GetComponent<NavMeshAgent>().enabled = true;


        }
        private Portal GetOtherPortal()
        {
            foreach (Portal portal in FindObjectsOfType<Portal>())
            {
                if (portal == this)
                {
                    continue;
                }
                if (portal.destination != destination) continue;
                return portal;

            }
            return null;
        }
    }

}

Check to make sure your portals are at the root of the heirarchy, not under another GameObject (it seems like a great idea to put all of a type of object under a parent object like a folder, but for Portals, this won’t work).

1 Like

Awesome . as you mentioned in my first scenes portals weren’t at the root of the hierarchy

Thanks a lot

This one is a tricky one because Unity is generating a yellow message (a warning) that DontDestroyOnLoad can’t be used no child GameObjects, but if you’re like me, you have yellow messages turned off because you’re tired of looking at messages warning that a field wasn’t initialized in the code when it’s a [SerializeField] to be set in the inspector. I asked a long time ago for Unity to escalate this warning to an error, but they never responded.

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms