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

Viewing 8 posts - 41 through 48 (of 48 total)
  • Author
    Posts
  • #17012
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::
    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?

    #17014
    Ser Apustaja
    Level 6
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    The MissingReferenceException you’re encountering indicates that the CanvasGroup object referenced by group has been destroyed, but your code is still trying to access it. This can happen if the object that the CanvasGroup component is attached to is destroyed before the coroutine completes.

    Here are a few suggestions to help you resolve this issue:

    1. Check if group is null before accessing it: You can add a null check before you try to access the alpha property of the group. This will help prevent the exception from being thrown if the CanvasGroup 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.

    2. 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.

    3. 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
      }
    4. 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}");
    5. 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.

    #17019
    MI NI
    Level 16
    Former Patron
    Helpful?
    Up
    0
    ::

    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?

    #17020
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    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.

    #17022
    MI NI
    Level 16
    Former Patron
    Helpful?
    Up
    0
    ::

    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))

    #17023
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    Can you update your project files again and send it to me? I will have another look.

    #17026
    MI NI
    Level 16
    Former Patron
    Helpful?
    Up
    0
    ::

    I have sent my project, please trouble you again

    #17044
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    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.

Viewing 8 posts - 41 through 48 (of 48 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: