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 /
  • Help Room /
avatar image
0
Question by Goldz · Oct 31, 2015 at 12:04 AM · custom editor

Help with custom EditorWindow

Hi I have i problem that has been bugging me for 2 days now i can't solve. So i have a custom class with 2 variables, and I want through custom item menu(class that inherits the EditorWindow class) to add elements to an array of this custom class, but i always get a NullReferenceException: Object reference not set to an instance of an object . If it is not an array, but a single variable of this custom class it works, but the moment i make it an array it gives this array. I make sure to initialize the array with new so this is not the problem.

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 HenryStrattonFW · Oct 31, 2015 at 12:49 AM 0
Share

Please share your code, its impossible to know whats wrong without seeing the code you are trying to run.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Statement · Oct 31, 2015 at 12:38 PM

Hi I have i problem that has been bugging me for 2 days now i can't solve. So i have a custom class with 2 variables.

 class CustomClass
 {
      object var1;
      object var2;
 }

and I want through custom item menu(class that inherits the EditorWindow class) to add elements to an array of this custom class,

 class CustomItemMenu : EditorWindow
 {
     CustomClass[] array = { }; // empty array of CustomClass
 
     [MenuItem("Window/Array test")]
     static void ShowWindow()
     {
         GetWindow<CustomItemMenu>().Show();
     }
 
     void OnGUI()
     {
         GUILayout.Label("array.Length: " + array.Length);
 
         if (GUILayout.Button("Add"))
         {
             // add element to array
             CustomClass[] newArray = new CustomClass[array.Length + 1];
             for (int i = 0; i < array.Length; ++i)
                 newArray[i] = array[i];
             newArray[array.Length] = new CustomClass();
             array = newArray;
         }
     }
 }

but i always get a NullReferenceException: Object reference not set to an instance of an object. If it is not an array, but a single variable of this custom class it works, but the moment i make it an array it gives this array. I make sure to initialize the array with new so this is not the problem.

Dunno, works for me. However, CustomClass is not serializable so the array will lose contents on script reload.

What exactly is null? The array? The objects in the array? The fields on the objects in the array?

If your custom class have a field of string type, Unity may automatically set null string to empty string when it loads your EditorWindow. However, when you create the object yourself, you are responsible of making sure all fields of CustomClass is set properly.

Comment
Add comment · Show 6 · 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 Goldz · Oct 31, 2015 at 03:28 PM 0
Share

What exactly is null? The array? The objects in the array? The fields on the objects in the array?>

That is the problem - I am not sure what is null. So i have this code: for(int i = 0; i < array.Length; i++){ /ex/ customClassArray[i].firstValue = EditorGUILayout.FloatField("First Value", customClassArray[i].firstValue); } and i get the Exception at line "ex", but if I do it like this for(int i = 0; i < array.Length; i++) { /ex/ customClassVar.firstValue = EditorGUILayout.FloatField("First Value", customClassVar.firstValue); } (not an array but just a single variable) it works.(also sorry i can't get the code to show properly)
avatar image Statement Goldz · Oct 31, 2015 at 03:36 PM 0
Share

I am not sure what is null.

Figure out what is null!

Check the error message. Double click the message and it'll highlight the line of code where the exception was thrown. If you have daisychained access like foo.bar.baz.dog = 3 then split that sausage up and test foo, foo.bar, foo.bar.baz individually..

Or hook up a debugger (monodevelop tutorial).

avatar image Statement Goldz · Oct 31, 2015 at 03:37 PM 0
Share

It looks as if you meant to include some code, but there is none.

avatar image Statement Statement · Oct 31, 2015 at 04:22 PM 1
Share

Okay.

The exception is thrown executing this line:

 customClassArray[i].firstValue = EditorGUILayout.FloatField("First Field", customClassArray[i].firstValue);

We know the array is not null. Why? Because you could access .Length in the for loop. customClassArray[i] could return null. Nothing else can throw a null reference exception from that line of code. So it means, your customClassArray contain null elements. Wherever you create your array, you need to populate the items in the array to something meaningful, or provide a default behaviour for null objects.

 var item = customClassArray[i];
 if (item == null)
 {
    GUILayout.Label("(null)");
 }
 else
 {
     item.firstValue = EditorGUILayout.FloatField("First Field", item.firstValue);
 }
Show more comments

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

33 People are following this question.

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

Related Questions

DrawGizmo - Accessing Editor script in DrawGizmo 1 Answer

Unity Custom Editor - Lauch callback at the end of the SerializeField. 0 Answers

Serialize child ScriptableObject asset values in parent ScriptableObject asset. 0 Answers

Unity 2019 LTS can't find scripts in "Editor" Folder. 0 Answers

Custom unity editor resetting values 0 Answers


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