StaticMeshComponenet Transforms nullptr, Access violation?

Hi, I have a StaticMeshComponene and when I get his world transforms, the component returned nullptr, why is it so?

DefaultConstructor

FVector StaticMesh01Location(-44.814232, -77.620117,  -0.000031);
FRotator StaticMesh01Rotation(0.000000, 0.000000, 0.000000);
FVector StaticMesh01Scale(1.000000, 1.000000, 1.000000);
StaticMesh01 = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh01"));
StaticMesh01->AttachToComponent(NewScene, FAttachmentTransformRules::KeepRelativeTransform);
StaticMesh01->SetRelativeLocation(StaticMesh01Location);
StaticMesh01->SetRelativeRotation(StaticMesh01Rotation);
StaticMesh01->SetRelativeScale3D(StaticMesh01Scale);

Function to return transforms

when called this function always retuned the StaticMesh01 is null

void AMyClass::GetAllTransforms(TArray<FTransform>& Transform)
{
	FTransform GetWorldTransform_01{};
	TArray<FTransform> MakeArray{};
	MakeArray.SetNum(1, true);
		
	//This line always returned a nullptr for getting world transforms from StaticMeshComponent
	GetWorldTransform_0 = StaticMesh01->USceneComponent::GetComponentTransform();
	
	MakeArray[0] = GetWorldTransform_0;
	
	Transform = MakeArray;
}

I’m not sure if that line is valid, first time seeing it used that way, usually this how I would access the transform
StaticMesh01->GetComponentTransform()

1 Like

let me try this, will update in minutes

no luck

is “new scene” a mesh or a scene component.
I also don’t see a root component.

Where are you actually using your component to set it on the transform ?
I don’t see anything of the sort there. You also don’t call on actor location ?
You need to get location to transform to world either of your mesh or thru your component. :slight_smile:

How are you going to store your possition to variable if you don’t have an actor location or a component location, or a mesh location, depending how you want to do this at what level, all work.

So what is it going to be your component location, you need to get that.
With that you get an Ftransform on it or an Fvector with location.

Instead of getting thins you are setting them ?

GetWorldTransform_0 = StaticMesh01->USceneComponent::GetComponentTransform();

This I take it should get you the location, but you first need to get a location and then call on the function with a second line.

So first
a constant FVector or a ftransform Location variable = your component ->Get Component magical function from unreal docs ();

second
Transform to relative position or world
Variable location that you are going to use = Get magical function tha transforms (). type of function (here you insert variable from step one that is an fvector or an ftransform);

You are missing elements.

First line the function to get location for your component
Second line the function to transform your location that you have now from first line.

The Root component is the scene componenet.

.h

UPROPERTY(EditAnywhere, BlueprintReadWrite)
USceneComponent* NewScene;

.cpp

NewScene = CreateDefaultSubobject <USceneComponent>("Scene Component");
RootComponent = NewScene;

You don’t have a location, why there for you get null result.

StaticMesh01->USceneComponent::GetComponentTransform();

This is for transform and location is where, you have to have a location first ? where do you get the location in a variable, you have to get the location of the component, and since you don’t have a root component you also might get offsets on the component because it’s not rooted.

FVector StaticMesh01Location(-44.814232, -77.620117, -0.000031);

already sets the location while constructing the components, and later in the get transform function I want to read that all data in world transform and get it

No, the scene component is not good for root component as it’s stated there.

Root = CreateDefaultSubobject< USceneComponent >(“USceneComponent”);

Your component var->AttachTo(Root)

You need to make the root component.
by the way where is the mesh attached to the component if that is the scene component there is no mesh, there is no mesh container ?
You have no mesh, just he container.

in blueprint it works fine without any problem.

Can you create a BP derived from this C++ class and check if you can see the Scene and static mesh components ?

I am getting the location of the container and later set the mesh dynamically, Sir my approach is working fine in blueprints, I override the scene component in blueprints and add 4 static meshes and successfully gets their world transform and dynamically sets the meshes later when i need to that location contains a container.

You don’t have a mesh loaded on the component and this will give null.
You don’t have a location on the component and this will give a null.

You first need to get a location on the component.

Derived class from this C++

This is the only thing you attach as I see in the constructor.

StaticMesh01->AttachToComponent(NewScene, FAttachmentTransformRules::KeepRelativeTransform);

So you are attaching to root your component but you don’t have the root component specified.

Then where is the mesh attached to the component in your constructor ? The mesh container ?

After this you need to get a location on one of them, on the actor, or on the component or on the mesh directly with GetLocation() function there many such functions depending how you want it.

Only after that you use that with a transform function to convert your possition that you got from step one.

Where are you using this class, is it placed on the map ? if it is remove it and add a new instance, sometimes it doesn’t refresh the class properly.
From your derived BP it looks good.

I would use SetupAttachment instead of AttachToComponent in constructor though.

they got the locations this is the constructor setup for the first mesh

FVector StaticMesh01Location(-44.814232, -77.620117,  -0.000031);
FRotator StaticMesh01Rotation(0.000000, 0.000000, 0.000000);
FVector StaticMesh01Scale(1.000000, 1.000000, 1.000000);

StaticMesh01 = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh01"));
StaticMesh01->AttachToComponent(NewScene, FAttachmentTransformRules::KeepRelativeTransform);
StaticMesh01->SetCollisionProfileName(TEXT("NoCollision"));

StaticMesh01->SetRelativeLocation(StaticMesh01Location);
StaticMesh01->SetRelativeRotation(StaticMesh01Rotation);
StaticMesh01->SetRelativeScale3D(StaticMesh01Scale);

and all other meshes

I have not placed this class in the level, I am placing it dynamically using

FTransform SpawnTransform; 
SpawnTransform.SetLocation(OutRow_LSN->Location);
ItemGroupRef = GetWorld()->SpawnActor<AItemGroup>(ItemGroup, SpawnLocation, SpawnInfo);

Yep you don’t have the static mesh container.
It’s pointer looks like this.
You can write it like this in the header.

UPROPERTY(EditAnywhere)
UStaticMesh* YourMeshContainer;

You use your constructor helper.
Then after that you say
YourMeshContainer = Mesh name variable from constructor helepr.Object;

Your static mesh is now loaded inside the container and next step is to load the container on the component.

Your Component ->SetStaticMesh(YourMeshContainer);
This will set the container that has the mesh on your component.

After this you can call the possition of either the mesh directly, actor directly or component to get location, once you have a location on it you can use a transform function (I don’t recomand using ftransform)

So you can use a transform function with the variable you have from where you got the location.

It gives null because it should since there is no location to begin with.

I’m glad this is happening, BP user :slight_smile:
C++ is a must.

1 Like

First time for some tutorial how to set things.

After this you will get to the part where you have to get location on things, since it’s normal to first get a location on the possition where things are.

1 Like