Forum begins after the advertisement:


[Part 5] Heart and Mana UI disappear after adding in the scene fader

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 5] Heart and Mana UI disappear after adding in the scene fader

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #19761
    Tú Hoàng Anh
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    Got everything looks perfect in the UI but when i add the Scene Fader and start the game, the Mana Container and the Heart Container just disappear and the Scene fader transition doesn’t even run when transition to another scene Here is the code i have written for the Scene Fader

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;
    
    public class SceneFaded : MonoBehaviour
    {
        [SerializeField] private float fadeTime;
    
        private Image fadeoutImage;
        public enum FadeDirection
        {
            In,
            Out
        }
        private void Awake()
        {
            fadeoutImage = GetComponent<Image>();
        }
    
        public void CallFadeandLoad(string _sceneLoad)
        {
            StartCoroutine(FadeandLoad(FadeDirection.In, _sceneLoad));
        }
    
        public IEnumerator Fade(FadeDirection _fadeDirection)
        {
            float _alpha = _fadeDirection == FadeDirection.Out ? 1 : 0;
            float fadeEnd = _fadeDirection == FadeDirection.Out ? 0 : 1;
    
            if (_fadeDirection == FadeDirection.Out)
            {
                while (_alpha > fadeEnd)
                {
                    SetColor(ref _alpha, _fadeDirection);
                    yield return null;
                }
                fadeoutImage.enabled = false;
            }
            else
            {
                fadeoutImage.enabled = true;
                while (_alpha < fadeEnd)
                {
                    SetColor(ref _alpha, _fadeDirection);
                    yield return null;
                }
            }
    
        }
    
        public IEnumerator FadeandLoad( FadeDirection _fadeDirection, string _sceneLoad)
        {
            fadeoutImage.enabled = true;
            yield return Fade(_fadeDirection);
            SceneManager.LoadScene(_sceneLoad);
        }
    
        void SetColor(ref float _alpha, FadeDirection _fadeDirection)
        {
            fadeoutImage.color = new Color(fadeoutImage.color.r, fadeoutImage.color.g, fadeoutImage.color.b, _alpha);
    
            _alpha += Time.deltaTime * (1 / fadeTime) * (_fadeDirection == FadeDirection.Out ? -1 : 1);
        }
    }

    Here is the code i have written for the UIManager

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class UIManager : MonoBehaviour
    {
        public static UIManager Instance;
    
        private void Awake()
        {
            if (Instance == null)
            {
                Instance = this;
                Destroy(gameObject);
            }
            else
            {
                Instance = this;
            }
            DontDestroyOnLoad(gameObject);
        }
    
        public SceneFaded sceneFaded;
    
        private void Start()
        {
            sceneFaded = GetComponentInChildren<SceneFaded>();
        }
    }
    
    #19762
    Sean Ng
    Level 7
    Moderator
    Helpful?
    Up
    0
    ::

    Hello Anh, when running your code the UI Manager keeps getting deleted, on your end when you run can you see if the UI Manager gets deleted? As within your UIManager.cs the Awake() it make the UI Manager a static instance but immediately destroy itself removing the UI Manager which in turns delete the health, mana and scene fade along with it.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class UIManager : MonoBehaviour
    {
        public static UIManager Instance;
    
        private void Awake()
        {
            if (Instance == null)
            {
                Instance = this;
                Destroy(gameObject);
            }
            else
            {
                Instance = this;
            }
            DontDestroyOnLoad(gameObject);
        }
    
        public SceneFaded sceneFaded;
    
        private void Start()
        {
            sceneFaded = GetComponentInChildren<SceneFaded>();
        }
    }

    Please change the portion highlighted above to the one below and it should be fixed

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class UIManager : MonoBehaviour
    {
        public static UIManager Instance;
    
        private void Awake()
        {
            if (Instance != null && Instance != this)
            {
                Destroy(gameObject);
            }
            else
            {
                Instance = this;
            }
            DontDestroyOnLoad(gameObject);
        }
    
        public SceneFaded sceneFaded;
    
        private void Start()
        {
            sceneFaded = GetComponentInChildren<SceneFaded>();
        }
    }
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: