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 2 days, 20 hours ago by Terence.
-
AuthorPosts
-
December 29, 2024 at 8:10 pm #16974::
I found that if the player dies, my monster will move forward uncontrollably, or it will be completely stuck and will not move, and the player cannot use healing health after death.
December 29, 2024 at 11:51 pm #16976::I will be improving on the health / mana UI and mechanics in the next part. Doing a stream on Tuesday.
EDIT: Regarding the healing, it should still be working. But you will need to hit some enemies to restore your mana first.
December 31, 2024 at 6:24 pm #16993::At the end of the video, I attacked monsters after resurrecting until I fully restored Mana. I jumped on the platform and tried to heal health, but it had no effect. Other functions could still be used normally.
January 1, 2025 at 8:31 pm #16994January 2, 2025 at 12:21 pm #16996::I tried to email you the cloud URL of the project, but it couldn’t be sent. Which email address should I send it to you from?
January 2, 2025 at 6:16 pm #17004January 3, 2025 at 12:24 am #17006::@mini, I found your issue. It’s not a problem with your healing. Your UI is not updating after you reset your character’s health back to 100 after respawn. So you are unable to heal because you are actually at max health.
Only after you get hit once by the enemy, the healthbar updates and everything will function normally afterwards.
View post on imgur.com
Make sure you update your healthbar when you respawn the player.
January 3, 2025 at 6:37 pm #17009::Thanks for your tip, I corrected him. But some problems occurred when I was testing other functions. I found that when I switched scenes, blackFade would not disappear and an error message would be generated. I didn’t know what to do.
January 3, 2025 at 10:59 pm #17010::Can you post your code that is causing the error here and specify the line where the MissingReferenceException is? The AI will be better suited to help you here.
Do a
print(name)
above thegroup.alpha
line (the one with the error) as well and tell me what the output is as well.January 3, 2025 at 11:21 pm #17011::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; print(name); <strong>group.alpha = (direction > 0 ? 1f - ratio : ratio) * originalAlpha;</strong> } group.alpha = direction > 0 ? originalAlpha : 0; if (direction < 0) gameObject.SetActive(false); isAnimating = false; }
View post on imgur.com
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))
-
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: