Upgrading to Unity 5.2 - All Image (Script) components now failing with NullReferenceException in GetStencilDepth?
I've just upgraded from 5.1 to 5.2 and now all Image (Script) components in one of my scenes are failing with a NullReferenceException in GetStencilDepth?
Anyone know how to fix this? I've tried deleting and reapplying the components, thinking the script behind might be an older version, but no joy. I've tried a few other things but can't get rid of the errors. Everything else seems to work fine but I can't get these images to display.
If anyone can shed any light on the cause or a possible fix that would be much appreciated!
I'm running into the same problems with my project. What used to work before is utterly broken, and this has caused me a lot of strife yesterday trying to figure it out. Good to know I'm not alone.
I hope that Unity comes out with a hotfix for this soon.
Answer by hacker64 · Sep 13, 2015 at 03:30 PM
We ran into the same problem with our project when upgrading to 5.2.
This error message appears to be caused by a Mask Component without a corresponding Image Component, as shown in the training video UI Mask.
As outlined in the video, while you do not have to have an actual Source Image selected, you do appear to need the Image Component, even if the Source Image is None. This isn't really clear in the text version of the docs.
Please note that the GameObject in the hierarchy that is linked to the Error message in the log may be a child of the GameObject that has the problem outlined above (as it was in our case), so you may have to look upwards in your hierarchy.
I would imagine there is some additional code now in 5.2 (in UnityEngine.UI.MaskUtilities.GetStencilDepth()) that is not checking for a null reference before using it, which can be fixed by Unity devs, but the root cause is actually our error (of not including the requisite Image Component).
Of course, adding a [RequireComponent(typeof(Image))] to the Mask Component would probably solve the issue altogether, since it seems now that one is required. ;-)
Thanks for weighing in here. I did already experiment with adding a mask component to all gameobjects which had an Image (Script) component. It didn't seem to help anything. Are you saying that will be the cause only if there is no source image? Because all of $$anonymous$$e have source images assigned.
It does seem to me that this is a bug in the GetStencilDepth function, introduced in 5.2.
Aha! No wait, you're quite right. I had a component further up the hierarchy that was using a mask but with no Image component. Added a blank Image component and everything fixed itself. That's a relief, I thought I'd have to reapply all of these images as sprites or something.
Nice one @hacker64. Problem solved.
I checked and none of my components use a mask but I get this error.
Whole project is broken as images throw this error at runtime.
Any one have any ideas how to fix it?
Thanks for the help. I hope they will fix this bug in the future versions.