Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by 3433 · Apr 10, 2013 at 03:40 AM · listinspectorcustom

Writing Editor Script, Getting Weird Null Reference Exception

I am writing a custom editor that will allow the user to add items to a list and print labels in the inspector for each item.

I declare the list as follows:

 public List<string> StageTable;

A bit later in the code I have this piece of code to create a label for each string in StageTable.

 foreach(string st in StageTable)
 {
             EditorGUILayout.LabelField("test string");
 }

I can see the result in the inspector, but when I try the game it blanks out the inspector where the result should be showing nothing there, and throws this long exception:

 NullReferenceException:
 Object reference not set to an instance of an object
 APG_CustomEditor.OnInspectorGUI
 () (at Assets/Editor/APG_CustomEditor.cs:53)
 UnityEditor.InspectorWindow.DrawEditors
 (Boolean isRepaintEvent, UnityEditor.Editor[]
 editors, Boolean eyeDropperDirty)
 (at C:/BuildAgent/work/14194e8ce88cdf47/Editor/Mono/ ...
 Inspector/InspectorWindow.cs:888)
 UnityEditor.InspectorWindow.OnGUI ()
 (at C:/BuildAgent/work/14194e8ce88cdf47/Editor/Mono/ ...
 Inspector/InspectorWindow.cs:243)
 System.Reflection.MonoMethod.Invoke (System.Object obj,
 BindingFlags invokeAttr, System.Reflection.Binder binder,
 System.Object[] parameters,
 System.Globalization.CultureInfo culture)

Any help or input on this matter is of course, most welcome!

Comment
Add comment · Show 2
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 3433 · Apr 10, 2013 at 08:33 AM 0
Share

Isnt declaring it with:

 public List StageTable;


And then adding to it with:

 StageTable.Add("stage");

Enough? I might be totally wrong here but it seemed to work when I created lists before.

avatar image Loius · Apr 10, 2013 at 08:38 AM 1
Share

new List();

spaces to trick Answers into not treating the like a html tag.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Chronos-L · Apr 10, 2013 at 06:17 AM

I think that the StageTable is not initialized when you try to built the LabelField for it. Null list and blank list is different, the first will produce nullref-exception and the second will be okay (just that there is no labelfield to be shown)

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 3433 · Apr 10, 2013 at 08:17 AM 0
Share

Basically I add to the list with this button:

if(GUILayout.Button("Add", GUILayout.Width (65), GUILayout.Height (20))) { StageTable.Add("stage"); }

Before doing the foreach I have an if statement to test that the list isnt empty:

 if(StageTable.Count > 0)

The full code is basically this:

using UnityEditor; using UnityEngine; using System.Collections.Generic; ///* [CustomEditor(typeof(AdaptableProceduralGenerator))] // ^ This is the script we are making a custom editor for. public class APG_CustomEditor : Editor {

 public List StageTable;
 
 private bool remConfirm = false;
 
 
 public override void OnInspectorGUI () {

     if(StageTable.Count > 0)
     {
         foreach(string st in StageTable)
         {
             EditorGUILayout.BeginHorizontal();
             bool showBtn = new bool();
             EditorGUILayout.Toggle(showBtn, GUILayout.Width (10), GUILayout.Height (20));
             EditorGUILayout.LabelField("Stage:",GUILayout.Width (42), GUILayout.Height (20));
             EditorGUILayout.EnumPopup(test.uponReachingEndState,GUILayout.Width (100), GUILayout.Height (20));
             EditorGUILayout.TextField("",GUILayout.Width (55), GUILayout.Height (20));
             EditorGUILayout.TextField("",GUILayout.Width (55), GUILayout.Height (20));
             bool showBtn2 = new bool();
             EditorGUILayout.Toggle(showBtn2, GUILayout.Width (10), GUILayout.Height (20));
             EditorGUILayout.EndHorizontal();
         }
     }
     if(GUILayout.Button("Add", GUILayout.Width (65), GUILayout.Height (20)))
     {
         StageTable.Add("stage");
     }

Hopefully this image will help explain whats hapenning: alt text

avatar image Chronos-L · Apr 10, 2013 at 08:37 AM 0
Share

It says "NullReferenceException: Object reference not set to an instance of an object APG_CustomEditor.OnInspectorGUI () (at Assets/Editor/APG_CustomEditor.cs:53)", but I can't see the line 53 in your code, and please highlight and Ctrl-$$anonymous$$ to format your code.

avatar image 3433 · Apr 10, 2013 at 08:39 AM 0
Share

Line 53 is this line:

 if(StageTable.Count > 0)
avatar image Chronos-L · Apr 10, 2013 at 08:44 AM 0
Share

It just mean that StageTable is null, for whatever reason there is out there.

avatar image 3433 · Apr 10, 2013 at 11:01 AM 0
Share

For some reason my other comment doesnt show up so I'll post it here: But if I initialize it from within the script it will constanly create a new list and I wont be able to add to it, which is what is going on now after I added:

List StageTable = new List();

Also if I add it after class declaration, I dont get null references and I can add to the list, but when I click play it empties the list.

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

13 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

Related Questions

A node in a childnode? 1 Answer

Custom list-item name 0 Answers

EditorGUI add SortingLayer-like list to custom Editor 1 Answer

Serialising subclass variables in inspector 2 Answers

Custom GUI Elements in Inspector 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