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.
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;
}
}
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.
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;
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. :)
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.
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.
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.
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.
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.
Thanks for helping as well. I'd like to give you a voucher for the asset too if you want. :)
Your answer
Follow this Question
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