- Home /
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!");
}
}
}
I just had a look at your questions. You have a lot (some really old) open questions. As the person who asked the questions you are responsible for selecting the correct answer. If there's no appropiate answer but you've solved it already yourself, please write your own answer and accept that one. If the question is no longer relevant / outdated you should close your question.
You want help from the community? Just be a part of it.
Answer by yosh · Oct 12, 2011 at 01:11 PM
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... ;)
Answer by Bunny83 · Aug 17, 2011 at 03:40 PM
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 ;)
@Bunny83: It was my fault. I´ve changed to build the bundle now with mainAssets.ToArray(). Also i´ve replaced the " if(mainAssets.ToArray().Length > 0)" to "mainAssets.Count. Unfortunately i get the same error message... (The "options[index]" code was trash and i deleted it.) Would be great if you still have a look...
Well, I'm pretty sure you get the error because you pass null as mainAsset to BuildPipeline.BuildAssetBundle. You should pass at least one of the objects. I haven't built assetbundles yet, but the examples don't suggest that you can pass null as main asset.
@Bunny83: Thanks for helping me out. I want create a texture bundle with lots of selected textures. So therefore i set the first "BuildPipeline.BuildAssetBundle" parameter "mainAsset" to null. I use the second parameter "assets" for my "Selection.objects".
I´ve also searched in the community and found something: http://answers.unity3d.com/questions/15705/what-is-mainasset-in-buildassetbundle.html
$$anonymous$$aybe there´s an other bug?
Answer by BrUnO-XaVIeR · Oct 27, 2012 at 11:18 PM
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.
You shouldn't call ExitGUI everytime. That wouldn't make much sense. You have to call it when you execute a function that affects the whole editor GUI, like SaveFilePanel or BuildAssetBundle. So put it at the end of your button body or where ever you cause the "GUI break"