Calling BeginPlay on actors in order within GameMode

I’m trying to gather up certain actors within my game world and call their respective BeginPlay() functions in order to ensure consistent construction order. This is my C++ code in GameMode:

DBZGameMode.h

#include "GameFramework/GameMode.h"
#include "DBZGameMode.generated.h"

class ADBZCameraFocusActor;
class ASDBZLevelScriptActor;

UCLASS()
class DBZGAME_API ADBZGameMode : public AGameMode
{
	GENERATED_BODY()
	
public:
	virtual void BeginPlay() override;
	
private:
	ADBZCameraFocusActor* cameraActor=nullptr;
	ASDBZLevelScriptActor* level=nullptr;
	
};

DBZGameMode.cpp

#include "DBZGame.h"
#include "Cameras/DBZCameraFocusActor.h"
#include "LevelScript/SDBZLevelScriptActor.h"
#include "DBZGameMode.h"


void ADBZGameMode::BeginPlay()
{
	Super::BeginPlay();

	for (TActorIterator<ADBZCameraFocusActor> actorItr(GetWorld()); actorItr; ++actorItr)
	{
		cameraActor = *actorItr;
	}
	for (TActorIterator<ASDBZLevelScriptActor> actorItr(GetWorld()); actorItr; ++actorItr)
	{
		level = *actorItr;
	}

	cameraActor->BeginPlay();
	level->BeginPlay();

}

as of right now the editor keeps crashing. There is only one instance of each in my game world (so they are blueprinted classes). My questions are is the editor crashing because I’m going about this in the wrong way? Or does it have something to do with my pointers? I feel my pointers should be fine so not sure if trying to call the BeginPlay() functions this way is a good thing? Thanks for any help.

Probably one of the iterators didn’t find a match and you’re not Null-guarding those calls to BeginPlay. Try:

if(cameraActor)
    cameraActor->BeginPlay();
if(level)
    level->BeginPlay();

If this code fixes the crash, then you have to figure out why it didn’t find them.

Note GameMode only exists on the server so be careful trying to access client data.

On a lesser note, you should ‘break;’ out of those actor iterators if you find a valid actor.