I am trying to use instanced static meshes in code. This is my current code to attempt to do so, when it gets down to AddInstance I get a blank exception in visual studio. This is an actor that is placed in the editor. I have also tried adding the instance in the BeginPlay function with the same results.
EDIT: I have determined it has something to do with the mesh. When I make the mesh null there is no crashing.
EDIT2: So I exposed the variables and set them through the editor. I still had the same crashes. So all I had left was removing the root node. It now doesn’t crash and adds instances, however I cant move it unless it has a root node.
ATerrainGenerator::ATerrainGenerator(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
RootNode = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("RootNode"));
RootComponent = RootNode;
RootNode->SetCollisionProfileName(UCollisionProfile::NoCollision_ProfileName);
static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeMesh(TEXT("StaticMesh'/Game/Meshes/Cube.Cube'"));
CubeInstances = ObjectInitializer.CreateDefaultSubobject<UInstancedStaticMeshComponent>(this, TEXT("CubeInstances"));
check(CubeInstances);
CubeInstances->AttachTo(RootComponent);
CubeInstances->SetStaticMesh(CubeMesh.Object);
FTransform InstanceTransform;
InstanceTransform.SetLocation(FVector(0.0f, 0.0f, 0.0f));
InstanceTransform.SetRotation(FQuat(0.0f, 0.0f, 0.0f, 0.0f));
InstanceTransform.SetScale3D(FVector(1.0f, 1.0f, 1.0f));
CubeInstances->AddInstance(InstanceTransform);
}