Fader wont fade in after loading scene from portal.cs

The fader wont FadeIn after the player loads into scene2 form a portal. So the tele works but i cant see anything untill i manually change the faders alpha to 0 in scene2. althuogh when i take the portal back to scene1 the fader works fine and everything visibly working

here is my portal.cs, please tell what else you need to see.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.AI;

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

        [SerializeField] int sceneToLoad = -1;
        [SerializeField] Transform spawnPoint;
        [SerializeField] DestinationIdentifier destination;
        [SerializeField] float fadeOutTime = 1f;
        [SerializeField] float fadeInTime = 0.5f;
        [SerializeField] float fadeWaitTime = 0.5f;
        // [SerializeField] string enemyTag;
        // List<GameObject> enemiesToKill = new List<GameObject>();
        
        

        // private void Start()
        // {
        //     Scene scene = SceneManager.GetActiveScene();
        //     DontDestroyOnLoad(gameObject);
        //     if(scene.name == "SandBox")
        //     {
        //         enemiesToKill.AddRange(GameObject.FindGameObjectsWithTag(enemyTag));
        //         foreach(GameObject i in enemiesToKill)
        //         {
        //             print(i);
        //         }
        //     }
        //     print(enemyTag);
        // }
        
        private void OnTriggerEnter(Collider other) {
            if (other.tag == "Player")
            {
                // if (enemiesToKill != null)
                // {
                //     if (enemiesToKill.Count == 0)
                //     {
                        StartCoroutine(Transition());
                //     }
                // }
            }
        }

        private IEnumerator Transition()
        {
            
            DontDestroyOnLoad(gameObject);

            Fader fader = FindObjectOfType<Fader>();
            SavingWrapper wrapper = FindObjectOfType<SavingWrapper>();
            yield return fader.FadeOut(fadeOutTime);
            print("FADEOUT COMPLETE");

            
            wrapper.Save();

            yield return SceneManager.LoadSceneAsync(sceneToLoad);

            wrapper.Load();

            Portal matchingPortal = GetMatchingPortal();    
            UpdatePlayer(matchingPortal);
            wrapper.Save();
            yield return new WaitForSeconds(fadeWaitTime);
            yield return fader.FadeIn(fadeInTime);
            print("FADEIN COMOPLETE");
            // yield return fader.FadeOutImmediate();

            //matchingPortal = null;
            Destroy(gameObject);
        }

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

            }
            return null;
        }

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

        // public void UpdateEnemyList(GameObject enemy)
        // {
        //     enemiesToKill.Remove(enemy);
        // }
    }
}

I noticed this same problem. However, when I traverse back and forth from Scene 1 and 2 the fader begins to work fine.

Note: This all happened after I deleted my “Save.sav” file. This was not an issue before it was deleted.

1 Like

ok ill try that

That didn’t seem to do that trick, thank you for helping though. I tested it more and when i save the game in scene1 take the portal into scene2 then manually fadeout the fader, Then i load the game(backtoscene1) the player get transported into space and this error displays

Failed to create agent because it is not close enough to the NavMesh
UnityEngine.Behaviour:set_enabled(Boolean)

It looks like there are a couple of things to unwrap here…
Besides the NavMesh warning (it’s just a warning), are there any red error messages when moving between scenes?

Based on what you’ve said about the fader needing to be manually reset in Scene 2, my prediction is that you actually already have a Fader in scene 2 that needs to be removed from the scene. The Fader should only exist in the persistent object prefab, not anywhere in the scene file.

If my prediction was correct, what’s happening is that the fader existing in scene 2 is fading out immediately, and when we move into the scene, the fader in the persistent object prefab is fading back in, but the fader already in the scene is still blocking.

For the second issue, the player getting transported into space… post the RestoreState() method in your Mover.cs, and we’ll take a look to see what might be going on.

1 Like

Thanks for helping me! There are no red error messages when moving btwn scenes. Should i take the persistantobjectspawner script of the core gameobject in scene2? (edit) so i took the POS off core in scene2 and i still get fader not fadingin.

here is my mover.cs

using UnityEngine;
using UnityEngine.AI;
using RPG.Core;
using RPG.Saving;
using RPG.Resources;

namespace RPG.Movement
{
    public class Mover : MonoBehaviour, IAction, ISaveable
    {
        // [SerializeField] Transform target;
        [SerializeField] float maxSpeed;
        // Ray lastRay;
        NavMeshAgent navMeshAgent;
        Health health;

        private void Start() {
            navMeshAgent = GetComponent<NavMeshAgent>();
            health = GetComponent<Health>();
        }

        // Update is called once per frame
        void Update()
        {
            navMeshAgent.enabled = !health.IsDead();
            UpdateAnimator();
        }

        public void StartMoveAction(Vector3 destination, float speedfraction)
        {
            GetComponent<ActionScheduler>().StartAction(this);
            MoveTo(destination, speedfraction);
        }

        public void MoveTo(Vector3 destination, float speedFraction)
        {
            navMeshAgent.destination = destination;
            navMeshAgent.speed = maxSpeed * Mathf.Clamp01(speedFraction);
            navMeshAgent.isStopped = false;
        }

        //MoveTo but its for crouching

        public void Cancel()
        {
            navMeshAgent.isStopped = true;
        }

        //Debug.DrawRay(lastRay.origin, lastRay.direction * 100);
        private void UpdateAnimator()
        {
            Vector3 velocity = navMeshAgent.velocity;
            Vector3 localVelocity = transform.InverseTransformDirection(velocity);
            GetComponent<Animator>().SetFloat("forwardSpeed", velocity.magnitude);
        }

        [System.Serializable]
        struct MoverSaveData
        {
            public SerializableVector3 position;
            public SerializableVector3 rotation;
        }

        public object CaptureState()
        {
            MoverSaveData data = new MoverSaveData();
            data.position = new SerializableVector3(transform.position);
            data.rotation = new SerializableVector3(transform.eulerAngles);
            return data;
        }

        public void RestoreState(object state)
        {
            MoverSaveData data = (MoverSaveData)state;
            GetComponent<NavMeshAgent>().enabled = false;
            transform.position = data.position.ToVector();
            transform.eulerAngles = data.rotation.ToVector();
            GetComponent<NavMeshAgent>().enabled = true;
        }
    }

}

thank you for your time

No, you still want the PersistentObject script (with the link to the PersistentObjects prefab), what you don’t want in your scene are the Fader, SavingSystem, or SavingWrapper scripts/prefabs. These should only exist in the PersistentObjects prefab. The PersistentObject script will pay attention and only load them if PesistentObject hasn’t run already.

1 Like

Ok, i have look through everything in scene2 while running the game and there is not a fader, savingsystem, or savingwrapper anywere besides the persistant object(clone). Is there somewhere else i should check? This is a screenshot,

Go ahead and zip up your project and upload it to https://gdev.tv/projectupload/ and I’ll take a look at it.

1 Like

thank you i will do that.

There were actually two different issues competing for attention in the project…
The first is in Fader.cs. We set you up for that one when we first created Fader, we cached the reference to the CanvasGroup in Start(), but the changes made when we added in the saving module means that many cached references need to be in Awake() instead of Start(). Simply rename this, and it will take care of the null reference.

The other problem is hidden in an obscure warning that DontDestroyOnLoad() gameobjects must be in the root, not a child object. In Scene 1, all of the portals are organized under a Portal gameobject. Once these are moved to the root of the heirarchy, I was able to navigate easily between the two scenes.

1 Like

YYYAAAY! you did it, no more bug and so fast. So the portals were breaking when we tried to save,load, and move between scenes becuase they aren’t in the root.

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

Privacy & Terms