Unknown Identifier DestroyImmediate()

I’m working on an editor script and I need to destroy a group of transforms that are parented to a single transform. In the past, I was able to simply call DestroyImmediate(parentTransform); but I’m guessing that the new strict rules in Unity 3.4 have killed that, as that method now gives me an ‘unknown identifier’ compile error. I changed it to Object.DestroyImmediate, which allows it to compile, but when the function is called, I get a Method Not Found error. What gives?

Object.DestroyImmediate(parentTransform);

Do you absolutely have to use DestroyImmediate?

Its highly recommended that you use Destroy instead (except for editor scripting)

Oh wow, that just flew over my head. My apologies.

I don’t have 3.4 yet, so I can’t do testing on my own.

The new ‘strict’ rules only apply to JavaScript in mobile (iOS or Android) projects. It sounds to me like its a bug and should be reported, but I would wait until you get a second opinion first.

For others to test (and perhaps you as well), add this code to TempEditor.cs under the Editor folder:

using UnityEngine;
using UnityEditor;
using System.Collections;

public class TempEditor : EditorWindow {
	
	GameObject obj;
	
	[MenuItem ("Window/TempEditor")]
	
	static void ShowWindow () {
		
		EditorWindow.GetWindow (typeof (TempEditor));
		
	}
	
	void OnGUI () {
		
		obj = (GameObject)EditorGUILayout.ObjectField (obj, typeof (GameObject), true);
		
		if (GUILayout.Button ("Delete")) {
                        if (obj != null)
			        DestroyImmediate (obj);
		}
		
	}
	
}

Which works under 3.3

According to the docs, the current method is obsolete. Unity - Scripting API: EditorGUILayout.ObjectField

I tried it using 3.4.

This should be working. :smile:

using UnityEngine;
using UnityEditor;
using System.Collections;

public class TempEditor: EditorWindow
{
    GameObject obj;

    [MenuItem("Window/TempEditor")]
    static void ShowWindow()
    {
        EditorWindow.GetWindow(typeof (TempEditor));
    }

    void OnGUI()
    {
        obj = (GameObject) EditorGUILayout.ObjectField(obj, typeof (GameObject), true);
        if (GUILayout.Button("Delete"))
        {
            if (obj != null)
            {
                DestroyImmediate(obj);
            }
        }
    }
}

I am not too sure what you want to achieve however it works when i drag a gameObject to the field and click delete. So in Unity 3.4 there’s a need to include true/false in your editoGUILayout.

Ok, it’s a bit of an odd thing, but I found the answer in this thread: http://forum.unity3d.com/threads/86032-change-object-via-script./page2
It turns out, because I was using my own custom subclass of Object, I can’t just call Destroy() since that’s something that you only get with monobehaviour. And when I called Object.Destroy(), it thought I meant System.Object instead of UnityEngine.Object.

Correct code, for those who want to know:

UnityEngine.Object.DestroyImmediate(destroyObject);

Thanks y’all for your help!