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 Nuclear Fox · Dec 06, 2012 at 03:23 PM · c#arraynull

Check if Array.GetValue(i) is null

I am trying to make sure that a public array field in a script is not empty and contains non-null elements. For this I am using C# reflection. I get the Array object, then run thru it's elements and check if they are null. Surprisingly, this doesn't work. When I step thru with the Mono debugger, the obj is shown to have value of null in Locals, however, the null == obj check always fails! The interesting part is that obj is actually shown as (null), but still with an arrow to the left, when I click on the arrow, the arrow just disappears.

 private void CheckArray(FieldInfo field)
     {
         Array array = field.GetValue(this) as Array;
         if (array.Length == 0)
         {
             if (!Attribute.IsDefined(field, typeof(CanBeEmpty)))
                 Debug.LogError(field.Name + " array empty in " + PrintScriptLocation() + ". Make sure you have filled the array in Inspector");
         }
         for(int i = 0; i < array.Length; ++i)
         {
             object obj = array.GetValue(i);
             Debug.Log("Array value: " + obj);
             if (null == obj)
             {
                 if (!Attribute.IsDefined(field, typeof(CanHaveNullElements)))
                     Debug.LogError("A null element found in array " + field.Name + " in " + PrintScriptLocation() + ". Make sure you set up the links to all array elemens in Inspector");
             }
             else
             {
                 int a = 5; // always lands here, even if obj was shown as (null) in Locals
             }
         }
     }

The same happens when I use fast enumeration like foreach (object element in array){..} . Even though the element is null, the check null == element always fails.

I am not an expert C#, but guess is that the object is actually not null, it's a pointer to some place which contains null itself. But why would Array.GetValue wrap the null in another object? Or is this a bug of Mono? Or C# script of Unity3d? Am I doing something wrong here?

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
1
Best Answer

Answer by MarkMizuguchi · Jan 03, 2013 at 07:43 PM

I ran into this issue as well. When using standard iteration techniques and search methods the test against null always returns false as you report. UnityEditor.ArrayUtility.IndexOf() behaves as expected though. So my workaround is:

 static bool ContainsNull<T>(T[] array)
     where T: class
 {
     int nullIndex = ArrayUtility.IndexOf(array, null);
     return nullIndex != -1;
 }

Under the hood IndexOf() is implemented as:

     public static int IndexOf(T[] array, T value)
     {
         List list = new List(array);
         return list.IndexOf(value);
     }

The conversion to a List is far from efficient which is why I suspect it is part of UnityEditor and not UnityEngine.

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 Nuclear Fox · Jan 03, 2013 at 09:34 PM 0
Share

Great, thanks. I've also seen Array.IndexOf(). Is it's implementation the same as ArrayUtility.IndexOf?

avatar image MarkMizuguchi · Jan 04, 2013 at 03:02 PM 0
Share

Array.IndexOf() has the same problem that you encountered and will not find the null values so it will always return -1 even if the array appears to contain nulls. For the exact implementation of Array.IndexOf() you can always download and exa$$anonymous$$e the $$anonymous$$ono source. If you need this functionality in game and not in just in the editor, simply copy & paste the IndexOf() implementation above into your own code.

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

10 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

Related Questions

Reorder array with nulls at the end 1 Answer

Multiple Cars not working 1 Answer

Array member treated as null even though it is not. 1 Answer

Distribute terrain in zones 3 Answers

Access if there are items in an array? 2 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