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
1
Question by ChrisSch · Oct 23, 2015 at 02:53 PM · editorarraynullreferenceexception

Fields not populated during OnValidate on Editor startup

So my question and script is pretty simple. I get null reference exception. I have some components I need to assign, and before I do that I need to check if there's particle systems in the array.

 public ParticleSystem[] particles;

 #if UNITY_EDITOR 
 void OnValidate() 
 {
     AssignParticleTurrets();
 }
 #endif
 
 void AssignParticleTurrets()
 {
     if(particles.Length < 1) //error on this line
         return;
 //the rest of the code
 }

Its as simple as that. As you can see I'm running this piece of code only inside editor. Could be why this weird nullexception is happening?

I want to check exactly what it says. Is there anything in the array, and if so, keep going, if not, do nothing. Because after the check, if its not empty, I'm using whats in the array, but it never gets that far, the error is at the very checking line.

Also tried (particles.Length == 0) and other variations doesn't work. I tried putting #endif down after the method as well, but nothing new. Even tried changing "particles" to something else in case its a keyword.

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 ChrisSch · Oct 23, 2015 at 11:00 PM 0
Share

Anyone got an idea how to check differently?

2 Replies

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

Answer by Statement · Oct 24, 2015 at 01:23 PM

Dunno, works for me, but I didn't restart the editor. I just slapped this on a game object and it had no problems.

What you could do is check references to the particles field, in case some other script it setting it to null. Also you can return early if it is null (or create a default array).

 using UnityEngine;
 
 public class Example : MonoBehaviour
 {
     public ParticleSystem[] particles;
 
     #if UNITY_EDITOR
     void OnValidate()
     {
         AssignParticleTurrets();
     }
     #endif
 
     void AssignParticleTurrets()
     {
         if (particles == null)
         {
             Debug.LogWarning("particles is null", this);
             return;
         }
 
         if (particles.Length < 1) //error on this line
             return;
     }
 }
Comment
Add comment · Show 7 · 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 ChrisSch · Oct 24, 2015 at 01:47 PM 0
Share

Really? No, nothing is modifying the particles array at all, in any script. Try saving and loading the scene. Forgot to mention it happens when the scene opens in editor, and even tho it works after the error, users still don't like errors in scripts they buy. If you know a way to hide the error that would be acceptable too I guess, but doesn't solve the mystery. :P

I'm using Unity 5.0.1f1 btw. Which one are you using if it works?

So weird, idk how to perform the check in a different way so it doesn't error. The stuff that happens after is just using the transforms where those particle systems are on, and adds a script component to them, so users don't have to do it manually. And that works fine after the error.

avatar image Statement · Oct 24, 2015 at 02:18 PM 0
Share

I use 5.2.1f1, but I haven't messed with the case when the user opens the editor. I know 5.2.1f1 has similar issues with EditorWindow values being overwritten after OnEnable which is a pain in the bread.

But if you get a null particles array, do as I said, return early from the function...

 if (particles == null)
     return;
avatar image ChrisSch · Oct 24, 2015 at 02:29 PM 0
Share

Holly... that worked! So checking if its null just checks if the variable exists at all? And from then on it works fine. Never thought of trying that. I'll remember that. Thanks!

You can convert it to an answer so I accept it. And also I wanna give you a voucher for the asset if you want. Just tell me where to send it. This is the asset in question. I can finally submit version 1.2 now, this little bug has been plaguing me for a few days. :)

avatar image Statement ChrisSch · Oct 24, 2015 at 04:29 PM 1
Share

Yes, that references can be null is very common, it's something you definitely should know as a programmer in your day to day program$$anonymous$$g. Obviously you can write code to ensure that you almost never have to do null reference testing but that is another topic.

That Unity actually allows that to be null in the first place however, is beyond my grasp. To me it sounds like a corner case they haven't tested unless there is a very good reason for why it is null when the editor loads up - just like my problem with EditorWindows variables beco$$anonymous$$g null after Awake and OnEnable have been executed, so my OnGUI calls crash.

Thanks Chris, but I am not in need of a turret system at the moment. I do appreciate your gesture though.

avatar image ChrisSch Statement · Oct 24, 2015 at 05:09 PM 0
Share

Yeah that's what I thought. Either untested or has a good reason. Could be built-in arrays only. I'm also checking a bool buy there's no errors there.

Show more comments
avatar image Statement ChrisSch · Oct 24, 2015 at 06:06 PM 0
Share

If you can make a small reproducible project with the simplest script example you can think of and reliably reproduce it in the latest version of Unity, I would file a bug report if I were you.

avatar image
1

Answer by TheRobWatling · Oct 23, 2015 at 03:10 PM

Does particles actually exist at this point? Checking the length of an array that doesn't exist / hasn't been initialised may give this behaviour.

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 ChrisSch · Oct 23, 2015 at 03:26 PM 0
Share

Yes its declared at the beginning with length of 0 with the rest of the variables. I even tried putting "#if UNITY_EDITOR " there too, but didn't help.

avatar image ChrisSch · Oct 24, 2015 at 02:41 PM 0
Share

Thanks for helping as well. I'd like to give you a voucher for the asset too if you want. :)

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

35 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 avatar image avatar image

Related Questions

Preventing an array from clearing when playing game. 2 Answers

How to change value of a null element in an array? 1 Answer

Object reference not set to an instance of an object + array of positions 1 Answer

C# Multidimensional arrays 0 Answers

UnityEngine.UI.Image keeps removing itself from a Prefab. 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