Editor-Script: NullReference after simple script execution

Hi,

i´ve written an editor script to create a texture bundle from my selection.
Everytime after execution i get the same error message:

NullReferenceException: Object reference not set to an instance of an
object at
UnityEditor.DockArea.EndOffsetArea ()
[0x00000] in :0
at UnityEditor.DockArea.OnGUI ()
[0x00000] in :0

Here´s my script.
Hope someone could help me where´s the bug…
Thanks for your time…

using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;

public class CreateTextureBundleForSelection : EditorWindow
{
	private List<Object> mainAssets;
	
	// Creates a bundle from all selected textures
	// 1. Select different textures in the project view
	// 2. Start execution) 
	[@MenuItem("AssetBuilding/Create bundle for selected textures...")]
    static void Init () {
		
		CreateTextureBundleForSelection window = (CreateTextureBundleForSelection)EditorWindow.GetWindow (typeof (CreateTextureBundleForSelection), false, "TextureBundle" );
		window.Show();
	}
	
	void OnGUI()
	{			
		EditorGUILayout.Space();
		GUILayout.Label ("Single textures will be packed to one bundle!", EditorStyles.boldLabel);
		GUILayout.Label ("Select textures from the project view.", EditorStyles.boldLabel);
		EditorGUILayout.Space();
		
		
		if(GUI.Button(new Rect(0, 70, position.width, 30), "Create texture bundle from selection"))
		{	
			mainAssets = new List<Object>();			

			foreach (Object tex in Selection.objects) 
			{
				string path = AssetDatabase.GetAssetPath(tex);				
				Object t = AssetDatabase.LoadMainAssetAtPath(path);	

				if (t != null){
					mainAssets.Add(t);
					Debug.Log("Path:   " + path);
                }
			}
			SaveAssetBundle();
		}
						
	}
	/// <summary>
	/// Save the assetbundle - bring up the panel for name description ==================================
	/// </summary>
	private void SaveAssetBundle()
	{
		if(mainAssets.ToArray().Length > 0)
		{
			string fileNameToSave = EditorUtility.SaveFilePanel ("Save Texture Bundle", "", "New Texturebundle Name", "unity3d");
			if (fileNameToSave.Length != 0)
			{
				BuildPipeline.BuildAssetBundle(null, Selection.objects, fileNameToSave, BuildAssetBundleOptions.CompleteAssets);				
				Debug.Log("---> ASSETBUNDLE BUILD COMPLETE! Objects of type: -->" + options[index] + "<--  has element count of: -->  " + Selection.objects.Length);
			}
		}
		else{
			Debug.Log("ERROR - No Assets!");	
		}				
	}
}

Hey, i´ve talked with Rune from Unity at the Unite 2011 about that problem. The solution is to call “EditorGUIUtility.ExitGUI();” at the end of the code. It works - no error messages… :wink:

I’m a bit confused. You create a List and add only valid assets into the list but you don’t use the list…

You still use BuildAssetBundle with Selection.objects which can contain also folders and other stuff.

The second thing is the List-class has it’s own Count property so converting the whole list temporarily into a native array to get the Length is unnecessary.

The last strange thing is “options[index]” in the debug.Log at the end. I can’t find options nor index somewhere.

I guess when you use your List to build the bundle, everything should be fine :wink:

I just ran into the same problem; But I couldn’t call “EditorGUIUtility.ExitGUI();” directly because panels’ layouts were becoming broken when I was using that. Than I’ve noticed when a EditorWindow does exist but its not visible, OnGUI() doesn’t receive any Event so I did this at the end of OnGUI():

if (Event.current == null) {EditorGUIUtility.ExitGUI();}

Than my GUILayout exits only if there is no OnGUI() events and now there’s no more “UnityEditor.DockArea.EndOffsetArea()” erros and no more broken GUILayouts.
Maybe this tip can help ppl causing erros when changing saved layouts where your custom window is referenced. Cheers.