Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by yosh · Aug 17, 2011 at 08:44 AM · editor-scriptingassetbundlenullreferenceexception

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!");    
         }                
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Aug 18, 2011 at 11:46 AM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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... ;)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Check_2015 · Feb 06, 2016 at 02:30 PM 0
Share

Worked for me too! Thanks for the Solution :D

avatar image
0

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yosh · Aug 18, 2011 at 09:08 AM 0
Share

Cool. Thanks i will check it out. Thanks for your hints...

avatar image yosh · Aug 18, 2011 at 02:30 PM 0
Share

@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...

avatar image Bunny83 · Aug 19, 2011 at 01:28 AM 0
Share

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.

avatar image yosh · Aug 19, 2011 at 08:06 AM 0
Share

@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?

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Oct 27, 2012 at 11:27 PM 0
Share

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"

avatar image Moor · Oct 22, 2013 at 11:33 AM 0
Share

undocumented

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to import the object from server to unity 2 Answers

Creating AssetBundles from Editor Scripts 1 Answer

Uncompressed Asset Bundle Still Has UnityFS tag and Thus Can't be Loaded With AssetBundle.LoadFromFile() 0 Answers

Change texture import settings by script 0 Answers

AssetBundle to prefab => meshes/textures missing 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges