c# - DontDestroyOnLoad 在我处于播放模式时不断调用并导致错误
问题描述
我正在制作一个 2d 无尽的跑步者,其中我已经实现了一些用于玩家重力和运动的代码。当我通过下面附加的脚本向玩家添加重力和速度并启用播放模式时,一个名为“DontDestroyOnLoad”的场景出现在层次结构窗口中,并且它有一个名为 [Debug Updater] 的对象作为子对象。播放器的脚本在这里:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HeroCharacterController : MonoBehaviour
{
[SerializeField] LayerMask groundLayers;
private float gravity = -50f;
private CharacterController characterController;
private Vector3 velocity;
private bool isGrounded;
// Start is called before the first frame update
void Start()
{
characterController = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
///Is Grounded
isGrounded = Physics.CheckSphere(transform.position, 0.1f, groundLayers, QueryTriggerInteraction.Ignore);
if(!isGrounded && velocity.y <0)
{
velocity.y = 0;
}
else
{
///Add Gravity
velocity.y += gravity * Time.deltaTime;
}
}
}
附加到调试更新程序的名为“DebugUpdater”的脚本在这里:
namespace UnityEngine.Rendering
{
class DebugUpdater : MonoBehaviour
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
static void RuntimeInit()
{
if (!Debug.isDebugBuild || FindObjectOfType<DebugUpdater>() != null)
return;
var go = new GameObject { name = "[Debug Updater]" };
go.AddComponent<DebugUpdater>();
DontDestroyOnLoad(go);
}
void Update()
{
DebugManager.instance.UpdateActions();
if (DebugManager.instance.GetAction(DebugAction.EnableDebugMenu) != 0.0f)
DebugManager.instance.displayRuntimeUI = !DebugManager.instance.displayRuntimeUI;
if (DebugManager.instance.displayRuntimeUI && DebugManager.instance.GetAction(DebugAction.ResetAll) != 0.0f)
DebugManager.instance.Reset();
}
}
}
这是导致的错误,我的脚本无法正常工作,因为播放器没有下降或移动。错误:
ArgumentException: SceneManager.SetActiveScene failed; the internal DontDestroyOnLoad scene cannot be set active.
UnityEngine.SceneManagement.SceneManager.SetActiveScene (UnityEngine.SceneManagement.Scene scene) (at <028e4d71153d4ed5ac6bee0dfc08aa3b>:0)
UnityEditor.SceneHierarchy.TreeViewItemDoubleClicked (System.Int32 instanceID) (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.IMGUI.Controls.TreeViewController.HandleUnusedMouseEventsForItem (UnityEngine.Rect rect, UnityEditor.IMGUI.Controls.TreeViewItem item, System.Int32 row) (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.IMGUI.Controls.TreeViewController.DoItemGUI (UnityEditor.IMGUI.Controls.TreeViewItem item, System.Int32 row, System.Single rowWidth, System.Boolean hasFocus) (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.IMGUI.Controls.TreeViewController.IterateVisibleItems (System.Int32 firstRow, System.Int32 numVisibleRows, System.Single rowWidth, System.Boolean hasFocus) (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.IMGUI.Controls.TreeViewController.OnGUI (UnityEngine.Rect rect, System.Int32 keyboardControlID) (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.SceneHierarchy.DoTreeView (System.Single searchPathHeight) (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.SceneHierarchy.OnGUI (UnityEngine.Rect rect) (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.SceneHierarchyWindow.DoSceneHierarchy () (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.SceneHierarchyWindow.OnGUI () (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
UnityEditor.HostView.Invoke (System.String methodName, System.Object obj) (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.HostView.Invoke (System.String methodName) (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.HostView.InvokeOnGUI (UnityEngine.Rect onGUIPosition, UnityEngine.Rect viewRect) (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.DockArea.DrawView (UnityEngine.Rect viewRect, UnityEngine.Rect dockAreaRect) (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.DockArea.OldOnGUI () (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEngine.UIElements.IMGUIContainer.DoOnGUI (UnityEngine.Event evt, UnityEngine.Matrix4x4 parentTransform, UnityEngine.Rect clippingRect, System.Boolean isComputingLayout, UnityEngine.Rect layoutSize, System.Action onGUIHandler, System.Boolean canAffectFocus) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.IMGUIContainer.HandleIMGUIEvent (UnityEngine.Event e, UnityEngine.Matrix4x4 worldTransform, UnityEngine.Rect clippingRect, System.Action onGUIHandler, System.Boolean canAffectFocus) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.IMGUIContainer.HandleIMGUIEvent (UnityEngine.Event e, System.Action onGUIHandler, System.Boolean canAffectFocus) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.IMGUIContainer.HandleIMGUIEvent (UnityEngine.Event e, System.Boolean canAffectFocus) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.IMGUIContainer.SendEventToIMGUI (UnityEngine.UIElements.EventBase evt, System.Boolean canAffectFocus) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.IMGUIContainer.HandleEvent (UnityEngine.UIElements.EventBase evt) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.EventDispatchUtilities.PropagateEvent (UnityEngine.UIElements.EventBase evt) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.MouseEventDispatchingStrategy.DispatchEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.EventDispatcher.ApplyDispatchingStrategies (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, System.Boolean imguiEventIsInitiallyUsed) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.EventDispatcher.ProcessEventQueue () (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.EventDispatcher.OpenGate () (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.EventDispatcherGate.Dispose () (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.EventDispatcher.Dispatch (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, UnityEngine.UIElements.DispatchMode dispatchMode) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.BaseVisualElementPanel.SendEvent (UnityEngine.UIElements.EventBase e, UnityEngine.UIElements.DispatchMode dispatchMode) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.UIElementsUtility.DoDispatch (UnityEngine.UIElements.BaseVisualElementPanel panel) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.UIElementsUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.GUIUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr) (at <023156577e4f4156adf0f4b3a3fedf85>:0)
解决方案
这会导致问题:
isGrounded = Physics.CheckSphere(transform.position, 0.1f, groundLayers, QueryTriggerInteraction.Ignore);
解决方案是对该方法仅使用两个参数:
Physics.CheckSphere(transform.position, 0.1f);
推荐阅读
- c# - 如果当前显示的文本编辑器更改,将引发什么事件
- javascript - 在 API 服务器上保存 html 元素
- reactjs - 如何解决生产中无样式内容的 Flash?
- excel - VBA自定义函数在子程序中工作但不在工作表中
- python - 用于类不平衡的 catboost 分类器?
- javascript - 我无法覆盖 React Bootstrap 组件
- java - Java - 读取和搜索 CSV 文件并显示结果
- javascript - Javascript拖放功能问题
- tsql - 捕获动态 T-SQL 的结果
- javascript - 如何根据我的数组填充 3 个下拉框和 1 个输入框?HTML Javascript