How to change TArray item?

Hello, I have a problem with changing background sprite position. I change it’s location like this
auto ex = Backgrounds[0]; ex->SetActorLocation(FVector(0.0f, -100.0f, positionZ)); positionZ++;

But when I tried to run this code, I UE crash with this error:

I think I should add my .h and .cpp files for more details
My header file:

UCLASS(minimalapi)
class APaperexamapleGameMode : public AGameMode
{
	GENERATED_BODY()
public:
	APaperexamapleGameMode();
	
	void BeginPlay() override;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Background sprties")
	TArray<ABackgroundSprite*> Backgrounds;

	void Tick(float deltaSeconds) override;

	float positionZ;
};

And my cpp file

void APaperexamapleGameMode::BeginPlay()
{
	positionZ = 0.0f;
	UWorld* const World = ();
	if(World)
	{
		for (size_t i = 0; i < 6; i++)
		{			
			//()->SpawnActor(ABackgroundSprite::StaticClass);
			Backgrounds.Add((ABackgroundSprite*) World->SpawnActor<ABackgroundSprite>(ABackgroundSprite::StaticClass())->SetActorLocation(FVector(2048.0*i, -100.0f, 0.0f)));			
		}
	}	
}

void APaperexamapleGameMode::Tick(float deltaSeconds)
{
	Super::Tick(deltaSeconds);
	auto ex = Backgrounds[0];
	ex->SetActorLocation(FVector(0.0f, -100.0f, positionZ));
	positionZ++;
}

You are using “ex” without verifying that it’s actually a valid object.

if(ex) ex->SetActorLocation(FVector(0.0f, -100.0f, positionZ));

Maybe your spawn does not work or another reason, but the crash is most certainly due to ex being NULL.

Thank you for answer, but it doesn’t help.

I found error, when I tried to access to my ABackgroundSprite I caught error: “read access violation”.

Maybe try adding this:

if( Backgrounds.Num() <= 0) return;

ex is not the array, I guess you meant Backgrounds.Num() <= 0 ?

opssssss ;p

Backgrounds is always not equal nullptr and it’s items count exactly that I need. As I wrote, I found error, it’s read access violation, when I tried to read Background items. What it cause?

On line 10 of your cpp file I see

Backgrounds.Add((ABackgroundSprite*) World->SpawnActor<ABackgroundSprite>(ABackgroundSprite::StaticClass())->SetActorLocation(FVector(2048.0*i, -100.0f, 0.0f)));

You are calling SetActorLocation inside Add. So you’re adding the result (a bool) casted to ABackgroundSprite*.

You could try first doing creation and initialization in a variable and then adding it to the array.