GetCachedWidget() crashes my project

I am slowly moving my project from UE4 (4.26) to UE5 (5.03) and am running into a very frustrating problem. I cant display my HUD in UE5. I keep getting an ‘EXCEPTION_ACCESS_VIOLATION reading address’ crash. The last call in the stack is always UWidget::GetCachedWidget(). I have tracked it down to a line in my HUD class where I call ToggleHUD function (called by the owning PlayerController). In ToggleHUD I simply say ‘MyWidget->IsVisible()’. This is where GetCachedWidget() crashes the program. MyWidget is not a nullptr, I have checked that.

The thing that bothers me the most is this:

If I play the game as a Singleplayer game the function works as expected and is fine.

If I play the game with 1 Listening Server and 1 Client; The Client works as expected, the Listening Server results in a crash

If I play the game with 1 Listening Server and 2 Clients; Client2 works, Client1 crashes, LS crashes

If I play with 1 Listening Server and 3 Clients; Client2 and Client3 works, Client1 and LS crash

If I play with 4 Clients and dedicated server; Client3 and Client 4 work, Client2 works but ONLY if Client3 has opened their HUD first (if not it crashes), and Client1 always crashes.

I cannot seem to find a pattern to any of this and its making it seem impossible to track down the actual problem. Again, I have made a check first to make sure MyWidget is not a nullptr and I have it print to the log if it is not. Every time its printing just fine. Is there any advice on how to further troubleshoot this? I’ve got the exact identical code in my UE4 project and it works just fine. UE5 seems to disagree.

So in case anyone stumbles onto this post with a similar issue I did solve it. I attempted to run the function through Blueprints to see if that gave me a similar issue. It turns out it didn’t and in fact, wrapping my Toggle() function as a UFUNCTION() to give it the BlueprintCallable specifier fixed the issue. Long story short, I guess if you’re having weird, inconsistent issues that make 0 sense, try adding UFUNCTION() to it.