Forum begins after the advertisement:


Scene got black and cannot transition to another scene

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity Scene got black and cannot transition to another scene

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #16072
    pepe cry
    Participant
    Helpful?
    Up
    0
    ::

    Hi everyone, I’m on the way to make the scene fading but got some trouble with it now.
    Firstly when I start the game, the image not was render under the game build like the video. Don’t know that does I miss anything in the video. Here is the scene fader of my
    Scene fading in canvas
    and here is the Inspector of it
    Inspector
    If i turn off the image and start the game, there is some error happend in the console
    Console log
    the first two error happand with start of the game. The third error happend when I get to the scene transition point. My character keep moving and the error happend.
    Here is the scene transition code

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    
    public class SceneTransition : MonoBehaviour
    {
        // Start is called before the first frame update
    
        [SerializeField] private string transitionTo;
        [SerializeField] private Transform startPoint;
        [SerializeField] private Vector2 directionExit;
        [SerializeField] private float exitTime;
    
        private void Start()
        {
            if (transitionTo == GameManager.Instance.transitionFromScene)
            {
                Move.Instance.transform.position = startPoint.position;
                StartCoroutine(Move.Instance.WalktonewScene(directionExit,exitTime));
            }
    
            StartCoroutine(UIManager.Instance.sceneFading.Fade(SceneFading.fadeDirection.Out));
    
        }
        private void OnTriggerEnter2D(Collider2D _other)
        {
            if (_other.CompareTag("Player"))
            {
                GameManager.Instance.transitionFromScene = SceneManager.GetActiveScene().name;
                Move.Instance.playerStateList.incutscene = true;
                StartCoroutine(UIManager.Instance.sceneFading.FadeAndLoadScene(SceneFading.fadeDirection.In,transitionTo));
            }
        }
    }
    

    the scenefading script

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;
    
    public class SceneFading : MonoBehaviour
    {
    
        [SerializeField] private float fadeTime;
        private Image fadeoutUIImage;
        public enum fadeDirection
        {
            In,
            Out
        }
        // Start is called before the first frame update
        void Awake()
        {
            fadeoutUIImage = GetComponent<Image>();
        }
    
        // Update is called once per frame
        void Update()
        {
            
        }
    
        public IEnumerator Fade(fadeDirection _fadeDirection)
        {
            float _alpha = _fadeDirection == fadeDirection.Out ? 1 : 0;
            float _fadeEndValue = _fadeDirection == fadeDirection.Out ? 0 : 1;
    
            if (_fadeDirection == fadeDirection.Out) 
            {
                while (_alpha >= _fadeEndValue) 
                {
                    setColorImage(ref _alpha,_fadeDirection);
                    yield return null;
                }
                fadeoutUIImage.enabled = false;
            }
            else
            {
                fadeoutUIImage.enabled = true;
                while(_alpha <= _fadeEndValue)
                {
                    setColorImage(ref _alpha,_fadeDirection);
                    yield return null;
                }
            }
            
        }
    
        public IEnumerator FadeAndLoadScene(fadeDirection _fadeDirection, string _sceneToLoad)
        {
            fadeoutUIImage.enabled = true ;
            yield return Fade(_fadeDirection);
            SceneManager.LoadScene(_sceneToLoad);
        }
    
        void setColorImage(ref float _alpha, fadeDirection _fadeDirection)
        {
            fadeoutUIImage.color = new Color(fadeoutUIImage.color.r, fadeoutUIImage.color.g, fadeoutUIImage.color.b, _alpha);
            _alpha += Time.deltaTime * (1/fadeTime) * (_fadeDirection == fadeDirection.Out ? -1 : 1);
        }
    }
    

    I already change the start from scene fading to the Awake but still cannot fix the null refference.
    Can you guys help me to make the black image render behind the game and fix this bug?

    #16077
    Chloe Lim
    Moderator
    Helpful?
    Up
    0
    ::

    Hi, from what I’ve seen from the image links, your scenefading image is disabled from the scene, thus giving the nullreference error, it should be enabled, and it will give a fade in every time a scene starts, and a fade out to fade in when transitioned. if the object is disabled, the other scripts that reference its components will not be able to find it

    #16082
    pepe cry
    Participant
    Helpful?
    Up
    0
    ::

    You are misunderstand it. I disable it so i could see what happend is in the game. Even if i enable it, the null reference still happend. It doesn’t because of disable it or not
    Uhh for some reason, after a day of not touching anything. The black image is now was rendering under the canvas and the error disapear. I don’t know why but now it just only have the third error. As you could see it in the video below
    Error at change scene point
    change scene error

    #16086
    Chloe Lim
    Moderator
    Helpful?
    Up
    0
    ::

    From what I’ve seen in the recording, the image component is not enabled again once the player triggers the collider, your FadeAndLoadScene seems fine, which means that the coroutine might be unable to start
    Maybe you can send your UI manager script so that I can get more clues?

    #16088
    pepe cry
    Participant
    Helpful?
    Up
    0
    ::

    Uhh for some reason, it start to dark again and I swear that i’m not touching anything from the last time I send you the record.
    error null ref
    null ref for scene fade
    As you could see in the newest record. The img was render over the game and i can still move my character behind it to the transition point and get the error. And yes it still null referrence
    for the UI mananger script, here is it

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class UIManager : MonoBehaviour
    {
        // Start is called before the first frame update
        public static UIManager Instance;
        private void Awake()
        {
            if (Instance != null && Instance != this)
            {
                Destroy(gameObject);
            }
            else
            {
                Instance = this;
            }
            DontDestroyOnLoad(gameObject);
        }
    
        public SceneFading sceneFading;
        public void Start()
        {
            sceneFading = GetComponent<SceneFading>();
        }
    }
    
    #16089
    Chloe Lim
    Moderator
    Helpful?
    Up
    0
    ::

    Ok, looking at the UIManager script, the scenefading in void start should be
    sceneFading = GetComponentInChildren<SceneFading>();
    this might be what is causing the null reference

    #16090
    pepe cry
    Participant
    Helpful?
    Up
    0
    ::

    It working now. The img is now was render under the game and fading very smooth when change scene. Very appreciate and thankful for your help

    #16095
    pepe cry
    Participant
    Helpful?
    Up
    0
    ::

    I know it is not the topic that I was asking but i’m kinda shy to make another topic. Can i ask for why my monster doesn’t move after I make the patrol like in the video part 6.
    I have the record here for you to see what i’m saying
    wolf error
    wolf error not moving
    And also it have a really weird error as you could see in the record. I will take a picture of that error for better vision
    weird error
    and here is the script of the enemy and wolf
    wolf script

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class WolfSpririt : Enemy
    {
        [SerializeField] private float flipWaittime;
        [SerializeField] private float ledgecheckX;
        [SerializeField] private float ledgecheckY;
        [SerializeField] private LayerMask whatIsGround;
        float timer;
        // Start is called before the first frame update
    
        protected override void Start()
        {
            base.Start();
            rb.gravityScale = 12.0f;
        }
    
        // Update is called once per frame
        protected override void Update()
        {
    
        }
    
        protected override void UpdateEnemyStates()
        {
    
            switch (currentState) 
            {
                case EnemyStates.Wolf_Idle:
                    Vector3 _ledgeCheckStartPoint = transform.localScale.x > 0 ? new Vector3(ledgecheckX, 0) : new Vector3(-ledgecheckX, 0);
                    Vector2 _wallCheckDir = transform.localScale.x > 0 ? transform.right : -transform.right;
                    if (!Physics2D.Raycast(transform.position + _ledgeCheckStartPoint, Vector2.down, ledgecheckY, whatIsGround) || Physics2D.Raycast(transform.position,
                        _wallCheckDir, ledgecheckX, whatIsGround))
                    {
                        ChangeState(EnemyStates.Wolf_Flip);
                    }
                    if (transform.localScale.x > 0)
                    {
                        rb.velocity = new Vector2(speed, rb.velocity.y);
                    }
                    else
                    {
                        rb.velocity = new Vector2(-speed, rb.velocity.y);
                    }
                    break;
                case EnemyStates.Wolf_Flip:
                    timer += Time.deltaTime;
                    if (timer > flipWaittime) 
                    {
                        timer = 0;
                        transform.localScale = new Vector2(transform.localScale.x * -1,transform.localScale.y);
                        ChangeState(EnemyStates.Wolf_Idle);
                    }
                    break;
    
            }
    
        }
    
    }
    

    enemy script

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Enemy : MonoBehaviour
    {
        [SerializeField] protected float health;
        [SerializeField] protected float recoilLength;
        [SerializeField] protected float recoilFactor;
        [SerializeField] protected bool isRecoilling = false;
    
        protected float recoinTimer;
        protected Rigidbody2D rb;
        [SerializeField] protected Move player;
        [SerializeField] protected float speed;
    
        [SerializeField] protected float dmgMake;
    
        // Start is called before the first frame update
    
        protected enum EnemyStates
        {
            Wolf_Idle,
            Wolf_Flip
        }
    
        protected EnemyStates currentState;
        protected virtual void Start()
        {
            rb = GetComponent<Rigidbody2D>();
            player = Move.Instance;
        }
    
        // Update is called once per frame
        protected virtual void Update()
        {
            UpdateEnemyStates();
    
            if (health <= 0)
            {
                Destroy(gameObject);
            }
            if (isRecoilling)
            {
                if (recoinTimer < recoilLength)
                {
                    recoinTimer += Time.deltaTime;
                }
                else
                {
                    isRecoilling = false ;
                    recoinTimer = 0 ;
                }
            }
            
        }
        public virtual void EnemyHit(float _DmgDone, Vector2 _hitDirection, float _hitForce)
        {
            health -= _DmgDone;
            if (!isRecoilling) 
            {
                rb.AddForce(-_hitForce * recoilFactor * _hitDirection);
                isRecoilling=true ;
            }
        }
    
        protected void OnTriggerStay2D(Collider2D _other)
        {
            if (_other.CompareTag("Player") && !Move.Instance.playerStateList.Invi)
            {
                Attack();
                Move.Instance.HitStopTime(0, 2, 0.5f);
            }
        }
    
        protected virtual void UpdateEnemyStates()
        {
    
        }
    
        protected void ChangeState(EnemyStates _newstate)
        {
            currentState = _newstate;
        }
        protected virtual void Attack()
        {
            Move.Instance.takeDmg(dmgMake);
        }
    }
    

    Could you help me or should i create another topic?

    #16100
    Chloe Lim
    Moderator
    Helpful?
    Up
    0
    ::

    the second error can be resolved by restarting unity, try to restart it first as the first error could be a start awake error

    #16101
    pepe cry
    Participant
    Helpful?
    Up
    0
    ::

    ok so I turn on Unity again but still the wolf is not moving at all but the second error really disapear. Did I misunderstanding your meaning?

    #16104
    Chloe Lim
    Moderator
    Helpful?
    Up
    0
    ::

    Is there any nullerror when the wolf is not moving?
    it could be possible that switch case is not being called, so use a print statement in updateenemystate to see if that is the issue

    #16105
    pepe cry
    Participant
    Helpful?
    Up
    0
    ::

    So i add 2 print line to the case code

    switch (currentState) 
    {
        case EnemyStates.Wolf_Idle:
            Vector3 _ledgeCheckStartPoint = transform.localScale.x > 0 ? new Vector3(ledgecheckX, 0) : new Vector3(-ledgecheckX, 0);
            Vector2 _wallCheckDir = transform.localScale.x > 0 ? transform.right : -transform.right;
            if (!Physics2D.Raycast(transform.position + _ledgeCheckStartPoint, Vector2.down, ledgecheckY, whatIsGround) || Physics2D.Raycast(transform.position,
                _wallCheckDir, ledgecheckX, whatIsGround))
            {
                ChangeState(EnemyStates.Wolf_Flip);
            }
            if (transform.localScale.x > 0)
            {
                rb.velocity = new Vector2(speed, rb.velocity.y);
            }
            else
            {
                rb.velocity = new Vector2(-speed, rb.velocity.y);
            }
            print("Idle");
            break;
        case EnemyStates.Wolf_Flip:
            timer += Time.deltaTime;
            if (timer > flipWaittime) 
            {
                timer = 0;
                transform.localScale = new Vector2(transform.localScale.x * -1,transform.localScale.y);
                ChangeState(EnemyStates.Wolf_Idle);
            }
            print("Flip");
            break;
    
    }

    and when i start the game, there is no null reference or error. Also the console doesn’t print any of the print in the code. What should I do now?

    #16106
    Chloe Lim
    Moderator
    Helpful?
    Up
    0
    ::

    Ok, there must be some issue with calling the UpdateEnemyStates() in the enemy script, could you check if at the top of the UpdateEnemyStates() function in enemy is being called?

    #16107
    pepe cry
    Participant
    Helpful?
    Up
    0
    ::

    In the Enemy script. Yes it has 2 refferences.
    enemy script
    but the UpdateEnemyStates() func doesnt has anything yet according to the first part of the part 6 video

    #16109
    Chloe Lim
    Moderator
    Helpful?
    Up
    0
    ::

    Ok, this might be the fix, remove the empty Update() function in your WolfSpirit Script, because it might be the one that is stopping the inherited Update() from the enemy script to be called

Viewing 15 posts - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: