Fix: Prevent FriendsListToggle null reference after scene changes#1297
Open
JessTello wants to merge 1 commit into
Open
Fix: Prevent FriendsListToggle null reference after scene changes#1297JessTello wants to merge 1 commit into
JessTello wants to merge 1 commit into
Conversation
JessTello
commented
Jun 11, 2026
Contributor
- Clear static Friends tab UI callbacks on scene unload to avoid retaining old listeners.
- Safely invoke Friends tab expand/collapse callbacks, skipping destroyed Unity targets.
- Guard optional UIFriendInteractionSource usage to prevent calls into missing or destroyed references during UI refresh.
- Clear static Friends tab UI callbacks on scene unload to avoid retaining old listeners. - Safely invoke Friends tab expand/collapse callbacks, skipping destroyed Unity targets. - Guard optional UIFriendInteractionSource usage to prevent calls into missing or destroyed references during UI refresh.
matt-clarke
requested changes
Jun 12, 2026
|
|
||
| public static void InvokeOnCollapseFriendsTab() | ||
| { | ||
| InvokeFriendsTabAction(ref OnCollapseFriendsTab); |
Contributor
There was a problem hiding this comment.
Just want to make the function names here a bit more generic
Suggested change
| InvokeFriendsTabAction(ref OnCollapseFriendsTab); | |
| SafeInvokeAction(ref OnCollapseFriendsTab); |
|
|
||
| public static void InvokeOnExpandFriendsTab() | ||
| { | ||
| InvokeFriendsTabAction(ref OnExpandFriendsTab); |
Contributor
There was a problem hiding this comment.
Suggested change
| InvokeFriendsTabAction(ref OnExpandFriendsTab); | |
| SafeInvokeAction(ref OnExpandFriendsTab); |
| [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] | ||
| private static void Initialize() | ||
| { | ||
| ClearFriendsTabActions(); |
Contributor
There was a problem hiding this comment.
Suggested change
| ClearFriendsTabActions(); | |
| ClearActions(); |
|
|
||
| private static void OnSceneUnloaded(Scene scene) | ||
| { | ||
| ClearFriendsTabActions(); |
Contributor
There was a problem hiding this comment.
Suggested change
| ClearFriendsTabActions(); | |
| ClearActions(); |
| ClearFriendsTabActions(); | ||
| } | ||
|
|
||
| private static void ClearFriendsTabActions() |
Contributor
There was a problem hiding this comment.
Suggested change
| private static void ClearFriendsTabActions() | |
| private static void ClearActions() |
| OnExpandFriendsTab = null; | ||
| } | ||
|
|
||
| private static void InvokeFriendsTabAction(ref Action action) |
Contributor
There was a problem hiding this comment.
Suggested change
| private static void InvokeFriendsTabAction(ref Action action) | |
| private static void SafeInvokeAction(ref Action action) |
| { | ||
| ClearFriendsTabActions(); | ||
| SceneManager.sceneUnloaded -= OnSceneUnloaded; | ||
| SceneManager.sceneUnloaded += OnSceneUnloaded; |
Contributor
There was a problem hiding this comment.
Are we able to remove this and fully rely on the null checking in the invoke function?
Clearing all the events when a scene is unloaded might not be a problem at the moment but there are cases where the subscription needs to survive:
- Objects marked with DontDestroyOnLoad
- Code that doesn't belong to a Unity object (e.g. static functions)
If multiple scenes are loaded at once at any point then when a scene unloads the actions are nulled too.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.