Accessing Game Instance = Crash


I made a new game instance object and configured the Unreal project settings to use it by default. Everything compiles fine.

In the game mode, I added the following:

UBlahGameInstance* BGI = Cast<UBlahGameInstance>(GetGameInstance());
UBlahGameInstance* BGI = Cast<UBlahGameInstance>(GetWorld()->GetGameInstance());

The code compiles fine but when I run the Unreal editor, it always crashes with the following error:

Access violation - code c0000005 (first/second chance not available)

Can anyone please tell me the correct way to access the game instance or is the above method correct?

Thank you.

The above method is correct, except one thing.
If you call this in constructor it will crash because constructors get called not only when you really “play” the game.
So when your GameMode get created there may be no World in it yet.
Add this to BeginPlay or PostInitializeComponents and it should work.