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
0
Question by invicticide · May 15, 2010 at 11:16 PM · editorarrayeditor-scriptingeditorwindoweditorgui

Member arrays not initialized in editor until after scripts are recompiled

I have a class, which inherits from MonoBehaviour, which includes a member array declared like so:

public EventNodeInfo[] m_eventNodeInfo;

If I create a new GameObject (in the editor) and assign this script to it, that array appears in the inspector with a default length of 0, as you would expect.

I also have a custom EditorWindow which contains controls that let me act on the contents of that array, for whichever GameObject is selected. One such control creates a new EventNodeInfo and appends it to the array.

If I create a new GameObject, assign my script, open my custom EditorWindow and click that "add" control, I get a null reference exception when I attempt to dereference "Length" out of the array reference on the newly-created object. The inspector shows this zero-length array just fine, but from my EditorWindow's perspective the array appears as null.

Now for the really weird part: if I touch a script (no changes to its content, just force-recompile), that new object's array is suddenly, magically okay! I can edit it through my custom EditorWindow as I would expect: the reference to the still-zero-length array is no longer null, and the world rejoices.

So um... what am I missing? O_o

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by qJake · May 16, 2010 at 12:18 AM

You're missing initialization of your array. Arrays are not magic, you cannot just resize them however you'd like. You have to instantiate them with a set size, and then go from there. Unity's Inspector just handles a lot of the hard work for you in terms of resizing an array, and it might be instantiating it for you even, I'm not sure.

You have two options. Either initialize your array, or use a List<> (which supports dynamic resizing).

You need something like this in Start() or Awake():

m_eventNodeInfo = new EventNodeInfo[10];

or whatever size you want your starting array to be. Each item is then filled in with a null value until you instantiate it.

You could also use a List<> generic, though I don't know how well those are supported by the Unity Inspector.

List<EVentNodeInfo> m_eventNodeInfo = new List<EVentNodeInfo>();

See the List documentation on MSDN for more information about Lists (which are vastly superior to arrays).

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

Any way to attach a bit of code to individual UI Windows [Editor] 1 Answer

Custom Prfab Editor Window 0 Answers

What is the best way to draw icons in Unity's Hierarchy window? 1 Answer

How do I code my own custom built blend tree node as seen in the Animation Controller Editor Window? 1 Answer

Editor Window Views 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