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
- This topic has 15 replies, 2 voices, and was last updated 1 month ago by pepecry.
-
AuthorPosts
-
October 14, 2024 at 7:17 pm #16072::
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
and here is the Inspector of it
If i turn off the image and start the game, there is some error happend in the console
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 codeusing 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?October 15, 2024 at 11:39 am #16077::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
October 15, 2024 at 5:01 pm #16082::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
change scene errorOctober 15, 2024 at 11:55 pm #16086::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?October 16, 2024 at 12:27 am #16088::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.
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 itusing 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>(); } }
October 16, 2024 at 1:24 am #16089::Ok, looking at the UIManager script, the scenefading in void start should be
sceneFading = GetComponentInChildren<SceneFading>();
this might be what is causing the null referenceOctober 16, 2024 at 1:38 am #16090::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
October 16, 2024 at 10:27 pm #16095::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 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
and here is the script of the enemy and wolf
wolf scriptusing 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?
October 17, 2024 at 11:15 am #16100::the second error can be resolved by restarting unity, try to restart it first as the first error could be a start awake error
October 17, 2024 at 5:26 pm #16101::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?
October 17, 2024 at 10:05 pm #16104::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 issueOctober 17, 2024 at 10:13 pm #16105::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?
October 18, 2024 at 11:07 am #16106::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?
October 18, 2024 at 3:11 pm #16107::In the Enemy script. Yes it has 2 refferences.
but the UpdateEnemyStates() func doesnt has anything yet according to the first part of the part 6 videoOctober 18, 2024 at 10:55 pm #16109 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: