SerializedObject target has been destroyed. UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)

As soon as I click on the “Keyboard” button in my scene it takes me to scene(1) and enables MovementKeyboard script as I wanted, but the MovementKeyboard never runs becasue of the error SerializedObject target has been destroyed. UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr). I have rebuild all the lightings but still in gives the same error as soon as the scene changes.

Script of button –

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class ControlKeyboard : MonoBehaviour {

    public MovementKeyboard ControlKEYBOARD;

    // Use this for initialization
    void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}

    public void ButtonKeyboard()
    {
        ControlKEYBOARD = ControlKEYBOARD.GetComponent<MovementKeyboard>();
        ControlKEYBOARD.enabled = true;

        SceneManager.LoadScene(1);
    }
}

MovementKeyboard Script attached to player-

public class MovementKeyboard : MonoBehaviour {

    public Rigidbody rbKeyboard;
    public float SidewaysForceKeyboard = 500f;
    public float ForwardForceKeyboard = 8000f;
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void FixedUpdate ()
    {
        //Forward Movement
        rbKeyboard.AddForce(0, 0, ForwardForceKeyboard * Time.deltaTime);

        //right movement
        if (Input.GetKey("d"))
        {
            rbKeyboard.AddForce(SidewaysForceKeyboard * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }

        //Left Movement
        if (Input.GetKey("a"))
        {
            rbKeyboard.AddForce(-SidewaysForceKeyboard * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }
    }
}

Please help me out. Thankyou
I have also attached the screenshots of the editor before and after clicking the button.

I had the same problem. It’s an editor error. I closed the editor and reopened it. Then everything was normal again.

You may have enabled the MovementKeyboard in the original scene before loading scene(1), and when you load a scene normally all the objects in the previous scene are destroyed. Take a look at Unity - Scripting API: Object.DontDestroyOnLoad. I think you just need to add that one line to the MovementKeyboard script (or any other script you attach to the player).

sorry for Necro this Thread.

I had the same problem and a restart from Unity helps. But from time to time it came back.
After a lot of testing around i found my problem. I use a wireless Keyboard and Mouse. every time when i get out of range or getting close to it the input delay raises and unity start to throw this error. even when i get back in range. only a restart will help until you lose connection again.

maybe this info will help someone.

testet with Unity 2019.3.14.f1

I had this error. I deleted my Library folder and let the game rebuild it. Problem went away.

For me the NavMeshAgent caused the problem. If the “Stopping Distance” was set to 0 Unity showed this error. After i set it to 0.1 it worked without error.

This happens to me when I have selected a prefab with a custom property drawer for a serializable object (that custom property drawer is showing in the inspector) AND then I reload the scene.

If I remove the selection and cause a scene reload, there is no error. Likewise, selecting a prefab without custom property drawers and reloading the scene causes no errors.

This tells me that this error is just a error when the prefab is reloaded in the scene; and it seems to be fairly benign, in that this error could not occur when the game is played within a release environment.

Just wanted to put the reason it was happening for me; This thread lead me to my answer…

Apparently the editor doesn’t like me editing a prefab in play mode while reloading a scene and throws the error:

SerializedObject target has been destroyed.

I had this error message pop up just today.
2019.4.15f1 (OSX)

This is the error that gets sent to console a bunch of times when I run the project (interwoven with my other console logs):

“SerializedObject target has been destroyed.
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)”

It certainly seems to be an editor issue in my case.

It only happens when these things are true:

  • I have a particular prefab selected in the Project tab. (It has a vanilla Mesh Renderer component on it. See attached image).
  • The Inspector tab is open and visible (not docked behind another tab)
  • The Inspector tab is in “Normal” rather than “Debug” mode
  • The Mesh Render component in the Inspector is not collapsed

If I hide the Inspector panel, collapse the Mesh Renderer component, or put the Inspector panel in Debug mode, the error messages no longer happen when I run the program. I’ve opened and closed the editor, deleted the Library folder, even resarted the computer… and in my case, this has not made the issue go away. A new project doesn’t do this, so something in my project is contributing, but the problem is a “phantom” one of some kind, since I can just do various UI things in the editor to make it stop.

I believe this is happening because some field in some SerializedObject is referencing a GameObject ( (which it must have been assigned to at some point during runtime) . Generally this is a big no-no, but maybe you’re using the “Runtime Set” pattern for instance.

The solution might be to null the GameObject reference when that GameObject is destroyed; in other words, whatever component is referencing the SciptableObject would need to null the SO’s GameObject reference on the component’s OnDestroy, or something like that.