Author Topic: NullReferenceException - UIPanel cachedTransform  (Read 4986 times)

Benzino07

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 1
  • Posts: 24
    • View Profile
NullReferenceException - UIPanel cachedTransform
« on: May 26, 2017, 11:43:20 AM »
Hi,

We are getting a NullReferenceException when the UIPanel attempts to get the cached transform. This only happens under certain conditions. This is the Exception:

  1. NullReferenceException
  2.   at UnityEngine.Component.get_transform () [0x00000] in <filename unknown>:0
  3.   at UIPanel.LateUpdate () [0x00000] in <filename unknown>:0

The only way I can get the issue to happen is if I show UIPanel A in Scene 1, destroy UIPanel A and then load Scene 2 (via additive), it will trigger the exception on another UIPanel in Scene 2 (always the same UIPanel, perhaps because it's first child of UIRoot). I have confirmed that UIPanel A is destroyed before Scene 2 is loaded, and that there are no objects referencing it (or it's  children). I see nothing wrong with UIPanel A, I can display it within Scene 2 and destroy it without any issue, likewise in Scene 1 (before loading Scene 2). There is no relation between the UIPanels involved in this scene either.

Once Scene 2 is loaded, Scene 1 is unloaded and both scenes have their own UIRoot, so that means for a short while, both scenes are active, and thus there are 2 active UIRoots. Is there a potential issue with having 2 UIRoots present (all be it in different scenes) at the same time? This problem is a bit of a headache, because the exception is thrown every update after it occurs and essentially breaks everything NGUI related.

We are using NGUI 3.11.1 (we can't upgrade due to the sorting changes which breaks stuff for us) and Unity 5.5.1.p1.

Thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NullReferenceException - UIPanel cachedTransform
« Reply #1 on: June 05, 2017, 03:31:25 AM »
Did you try to immediately update draw calls after destroying the UI, and before second scene loads? NGUITools.ImmediatelyCreateDrawCalls

The whole scene system change in Unity 5 was always iffy with NGUI. The error message you posted actually suggests that UIPanel script was there, yet the transform was already destroyed. How can transform be destroyed without other components on the same object being destroyed? It's basically some Unity glitch from my understanding that deals with their system of handling multiple additive scenes.

Generally forcing NGUI to update draw calls immediately before trying to use Unity's scenes should fix the issue.

Benzino07

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 1
  • Posts: 24
    • View Profile
Re: NullReferenceException - UIPanel cachedTransform
« Reply #2 on: June 06, 2017, 03:46:00 AM »
I will give that a go and report back, thanks.

Benzino07

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 1
  • Posts: 24
    • View Profile
Re: NullReferenceException - UIPanel cachedTransform
« Reply #3 on: June 06, 2017, 08:20:42 AM »
Unfortunately that did not work, it produces the same error.

Benzino07

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 1
  • Posts: 24
    • View Profile
Re: NullReferenceException - UIPanel cachedTransform
« Reply #4 on: June 06, 2017, 10:13:21 AM »
One thing we have discovered is that when the issue occurs, minimising the app (I should have mentioned this is an iOS app) and bringing it back to the foreground fixes the issue. So perhaps I need to call ImmediatelyCreateDrawCalls at a different time.

So I was hoping to apply a quick fix by simply removing the UIPanel A and then investigate the issue at a future date when not restricted to a deadline. But it appears the issue can occur if any UIPanel is created and destroyed in Scene 1 before loading Scene 2, so I'll be investigating this more and will update this thread in case it's useful for others in the future.

Benzino07

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 1
  • Posts: 24
    • View Profile
Re: NullReferenceException - UIPanel cachedTransform
« Reply #5 on: June 07, 2017, 11:40:49 AM »
After further work on this, I don't think ImmediatelyCreateDrawCalls is going to do the trick. I've tried calling it on the UI Root in Scene A after the UIPanel is destroyed, I have also tried calling it on the UIRoot in Scene B during Awake, Start etc and the issue still occurs (plus it also moves the UIRoot for some reason). I have also tried it on the UIPanel in Scene B that the exception is thrown on, but again it does not fix the issue.

What I find very interesting is that minimising the app and bringing it back to the foreground fixes the issue. I'm not sure why that is, nor can I see any NGUI code been executed in OnApplicationPause that could be fixing the issue. Have you any idea why this works or is it just a Unity engine doing stuff in the background?

So instead I have tried calling Refresh() and RebuildAllDrawCalls() for the UIPanel which the exception is thrown on.

Benzino07

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 1
  • Posts: 24
    • View Profile
Re: NullReferenceException - UIPanel cachedTransform
« Reply #6 on: June 08, 2017, 11:40:55 AM »
Ok, so I finally have a solution. I found other people had the same issue before (not sure how I didn't see the threads before), namely this one.

Applying the fix mentioned in that thread fixed the issue. However, the same sort of issue started happening if no UIPanel was shown in Scene A before Scene B. So to fix that issue, I call the following code when Scene A is loaded and then again when Scene B is unloaded. Both these fixes together seem to have solved the issue for us.

  1. private void ResetPanelDrawCalls()
  2. {
  3.         // Release inactive draw calls
  4.         UIDrawCall.ReleaseInactive();
  5.  
  6.         // Set the Panel as dirty so draw calls are rebuilt
  7.         _screenUIPanel.SetDirty();
  8. }

There is definitely something wonky going on when loading scenes with Addictive Loading, but Addictive Loading is required for us to smoothly load the game so dropping it was not an option. Hopefully this is of some use to others encountering the issue. I hope I never see this issue again :(

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NullReferenceException - UIPanel cachedTransform
« Reply #7 on: June 10, 2017, 06:38:38 AM »
Personally I tend to bypass as much Unity code as I can possibly can. I did my own additive scene loading the same way I did my own nested prefabs -- by relying on my own code instead of Unity's. The more I rely on Unity, the more likely it is to break from version to version (dynamic fonts, wheel physics), or simply be ungodly slow (terrain, grass, static / dynamic batching, etc).

I basically load an empty scene, then instantiate objects dynamically. This works for me because most of my game's content is procedurally generated, so I understand that won't work for all projects.

Benzino07

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 1
  • Posts: 24
    • View Profile
Re: NullReferenceException - UIPanel cachedTransform
« Reply #8 on: June 12, 2017, 05:42:03 AM »
Yeah, that is good advice alright, unfortunately not something we can do in this instance. Hopefully this thread is useful for others who encounter the issue too.