Forum begins after the advertisement:
[Part12] Regarding some operational issues in the article
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [Part12] Regarding some operational issues in the article
- This topic has 46 replies, 4 voices, and was last updated 7 months, 3 weeks ago by
Terence.
-
AuthorPosts
-
January 4, 2025 at 12:58 am #17012::
MissingReferenceException: The object of type 'CanvasGroup' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. UIScreen+<Fade>d__24.MoveNext () (at Assets/Script/UI/UIScreen.cs:180) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <3b24cc7fa9794ed8ab04312c53e6dedd>:0)
protected virtual IEnumerator Fade(float duration, int direction = 1) { WaitForSecondsRealtime w = new WaitForSecondsRealtime(updateFrequency); // Don't play as long as another animation is playing on this. while (isAnimating) yield return w; isAnimating = true; float currentDuration = duration; group.alpha = direction > 0 ? 0 : originalAlpha; gameObject.SetActive(true); while (currentDuration > 0) { yield return w; float timeScale = GetTimeScale(); if (timeScale <= 0) continue; currentDuration -= w.waitTime * timeScale; float ratio = currentDuration / duration; group.alpha = (direction > 0 ? 1f - ratio : ratio) * originalAlpha; } group.alpha = direction > 0 ? originalAlpha : 0; if (direction < 0) gameObject.SetActive(false); isAnimating = false; }
View post on imgur.com
@Ser.Apustaja, the line above
group.alpha = (direction > 0 ? 1f - ratio : ratio) * originalAlpha;
is giving a missing reference exception as shown above. Any chance you can give an indication as to why? Or what we should look at?January 4, 2025 at 2:11 am #17014::The
MissingReferenceException
you’re encountering indicates that theCanvasGroup
object referenced bygroup
has been destroyed, but your code is still trying to access it. This can happen if the object that theCanvasGroup
component is attached to is destroyed before the coroutine completes.Here are a few suggestions to help you resolve this issue:
-
Check if
group
is null before accessing it: You can add a null check before you try to access thealpha
property of thegroup
. This will help prevent the exception from being thrown if theCanvasGroup
has been destroyed.if (group == null) yield break; // Exit the coroutine if group is null
You can place this check at the beginning of your
Fade
coroutine. -
Ensure the GameObject is not destroyed prematurely: Review your code to ensure that the GameObject that holds the
CanvasGroup
component is not being destroyed before the coroutine finishes. If you are destroying the GameObject during the fade process, you may want to rethink your logic to ensure it remains alive for the duration of the fade. -
Use a try-catch block: If you want to handle the exception gracefully, you can wrap the code that accesses
group.alpha
in a try-catch block. However, this is more of a workaround and not a best practice for handling such issues.try { group.alpha = (direction > 0 ? 1f - ratio : ratio) * originalAlpha; } catch (MissingReferenceException) { yield break; // Exit the coroutine if an exception is caught }
-
Debugging: Add debugging logs to check if
group
is becoming null at any point. This can help you identify where the destruction is occurring.Debug.Log($"Current group: {group}");
-
Check the flow of your game logic: Make sure that there are no other scripts or game events that might be destroying the GameObject or the
CanvasGroup
while the fade coroutine is still running.
By implementing these suggestions, you should be able to address the
MissingReferenceException
you’re encountering.January 5, 2025 at 1:30 pm #17019::I tried the above suggestions but I still don’t understand what the problem is. Is there any step I am missing on Unity that causes this problem?
January 5, 2025 at 2:50 pm #17020::Check the action assigned to your Quit to Main Menu button. Make sure that you are using LoadScene from the UI Manager instead of the Pause Menu, because if the Pause Menu gets disabled the switching of scenes stops.
January 5, 2025 at 4:37 pm #17022::Yes, my QuitToMainMenu uses UIManager’s LoadScene, but my problem is that the Fader (Temp) will not be deleted when I switch scenes, but the player can still move freely, including using the pause menu, but the screen will be black. (Fader(Temp))
January 5, 2025 at 6:14 pm #17023January 5, 2025 at 6:35 pm #17026January 6, 2025 at 1:42 pm #17044::Managed to fix your problem. The Fader is coming from your UIManager’s entrance animation. I modified your UIManager’s Awake function so that it calls
base.Awake()
only if it is not destroyed. Otherwise, if you have duplicate UIManagers on the Scene, they will trigger the entrance fade and get destroyed before they are able to clean up the fade animation.public class UIManager : UIScreen { public static UIManager Instance; //¨¾¤î¦hÓ«½Æªºª±®a¸}¥»¥X²{ [Header("UI Manager")] public GameObject mapHandler; //¦a¹Ï public GameObject inventory; //I¥] public UIScreen deathScreen; protected override void Awake() { if (Instance != null && Instance != this) { Destroy(gameObject); return; } base.Awake(); DontDestroyOnLoad(gameObject); Instance = this; //new } }
One more thing as well: You don’t need an entrance animation for your UIScreen, because the SceneTransition script already handles the fade in / fade out.
-
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: