MissingReferenceException: The object of type ‘CanvasRenderer’ 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.
UnityEngine.UI.GraphicRaycaster.Raycast (UnityEngine.Canvas canvas, UnityEngine.Camera eventCamera, UnityEngine.Vector2 pointerPosition, System.Collections.Generic.IList1[T] foundGraphics, System.Collections.Generic.List
1[T] results) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/GraphicRaycaster.cs:335)
UnityEngine.UI.GraphicRaycaster.Raycast (UnityEngine.EventSystems.PointerEventData eventData, System.Collections.Generic.List1[T] resultAppendList) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/GraphicRaycaster.cs:236) UnityEngine.EventSystems.EventSystem.RaycastAll (UnityEngine.EventSystems.PointerEventData eventData, System.Collections.Generic.List
1[T] raycastResults) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:263)
UnityEngine.InputSystem.UI.InputSystemUIInputModule.PerformRaycast (UnityEngine.InputSystem.UI.ExtendedPointerEventData eventData) (at Library/PackageCache/com.unity.inputsystem@1.4.4/InputSystem/Plugins/UI/InputSystemUIInputModule.cs:271)
UnityEngine.InputSystem.UI.InputSystemUIInputModule.ProcessPointer (UnityEngine.InputSystem.UI.PointerModel& state) (at Library/PackageCache/com.unity.inputsystem@1.4.4/InputSystem/Plugins/UI/InputSystemUIInputModule.cs:315)
UnityEngine.InputSystem.UI.InputSystemUIInputModule.Process () (at Library/PackageCache/com.unity.inputsystem@1.4.4/InputSystem/Plugins/UI/InputSystemUIInputModule.cs:2112)
UnityEngine.EventSystems.EventSystem.Update () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)
先上報錯問題。然后說一下我怎么遇到的問題,最后再說解決辦法。
這個問題我是怎么遇到的呢,刪除不用的UI,這里是一個功能模塊的UI,比如你的程序的主頁UI,刪除后直接運行,當你的鼠標憋說點擊了,放到UI上就會報上邊的錯誤,跟你說GraphicRaycaster.Raycast檢測不到你的UI了。或者說你的UI上沒有CanvasRenderer了,問你是不是刪除了,對,我是刪除了,我tm在程序啟動前刪除的,反應遲鈍把,這不得不讓你想unity的緩存機制了。問題找到了,給你的感覺就是他緩存里你的東西還在,但是你刪除了,出現的是這個問題。
一般情況出現這個問題比如說你已經刪除了,但是找不到,說明有地方有殘留,比如你的腳本上有對刪除物體的引用,有對事件的注冊等等,其實不一定非得是CanvasRenderer的missing的報錯,其他組件有missing的報錯都可以這么推理。不是你腳本上的問題,那么就是unity的緩存的問題。
接下來說一下我的解決辦法:
直接刪除你不要的UI,他還是報錯怎么辦?把unity關閉,然后重啟
最后在說一下,出現這個問題的原因之一是你之前肯定看到了說你的UI預制體too large的紅色報錯了。