Unable to log within for loop without Exception Access Violation

After trying multiple different things I am not sure how to proceed.
When I have the log within the for loop it crashes.

This happens when the count is declared in the loop and outside the loop.
And when the UE_LOG is inside the loop referencing nothing and just printing plain text.
when the Log is outside the loop it works just fine.

Everything else is working as expected.

If this is something really obvious I’m sorry, this is my first week and a half with unreal and c++.


// Fill out your copyright notice in the Description page of Project Settings.

#include "Spawner.h"
#include "Engine/World.h"
#include "TestObject.h"
// Sets default values
ASpawner::ASpawner()
{
    // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;
    SLoc =  CreateDefaultSubobject<UArrowComponent>(TEXT("Start Location"));
    RootComponent = SLoc;
}
// Called when the game starts or when spawned
void ASpawner::BeginPlay()
{
    Super::BeginPlay();
    MapSpawn();
}
void ASpawner::MapSpawn()
{
    SVarA = 5;
    GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Red, TEXT("Actor Spawning - Spawner"));
    int32 count = 0;

    for(;count < SVarA; count++ )
    {
        FTransform SpawnLocation;
        GetWorld()->SpawnActor<ATestObject>(ATestObject::StaticClass(), SpawnLocation);
        SpawnLocation = TestObject->GetActorTransform();
        // UE_LOG(LogTemp, Warning, TEXT("NUMBER: %i"), count); <== THIS OR THIS ==>  UE_LOG(LogTemp, Warning, TEXT("NUMBER: ")); RESULTS IN A CRASH
    }
    UE_LOG(LogTemp, Warning, TEXT("NUMBER: %i"), count); // THIS IS FINE

}
// Called every frame
void ASpawner::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
}



What are you getting as the crash? This particular line or somewhere further down the callstack? Any logs to narrow down where it’s unhappy? Also, I believe the format for an integer is %d – I didn’t know %i worked.

Not sure what happened but I cant replicate the error today. And %i gives you a integer ( 1 ) and %d gives you a float (1.00 ) I think. Thanks anyway mate.



GetWorld()->SpawnActor<ATestObject>(ATestObject::StaticClass(), SpawnLocation);
SpawnLocation = TestObject->GetActorTransform();


You’re spawning ATestObject and using it without checking that it’s actually been spawned or is valid, that’s the problem. It’s not being spawned.

Access Violation usually means you are dereferencing a nullptr.

“Object reference not set to an instance of an object” for the new people coming to Unreal.
That phrase they are going to understand quicker :stuck_out_tongue: