Home > Enterprise >  DontDestroyOnLoad keeps calling and causes error when I am in play mode
DontDestroyOnLoad keeps calling and causes error when I am in play mode

Time:10-24

I am making a 2d endless runner in which I have implemented some code for gravity and movement of player. When I added gravity and velocity to player via a script attached below and enabled the play mode a scene named "DontDestroyOnLoad" appears in the hierarchy window and it has an object named [Debug Updater] as a child attached to it. The script for player is here:

 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;
    }
}
}

The script named "DebugUpdater" attached to the Debug Updater is here:

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();
        }
    }
}

This is the error being caused and my script is not working as in the player is not falling or moving. The error:

 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)

Moreover I have also attached the screenshot of my game in play mode as well as in scene mode. Screenshots: Scene viewPlay mode Thankyou in Advance!

CodePudding user response:

This causes the problem:
isGrounded = Physics.CheckSphere(transform.position, 0.1f, groundLayers, QueryTriggerInteraction.Ignore);

The solution is to use only two arguments for this method:
Physics.CheckSphere(transform.position, 0.1f);

  • Related