NewObject name setting?

hi :smiley:

currently i’m trying to make a grid generate system and it is my first project on unreal.
generator class has 3 variables which for number X, Y and size of grid block.
and based on these values create and visualize grid via Instanced Static Mesh Component.

but i have some trouble on creating component :confused:

i followed this post and result is like this

ABasicGrid::ABasicGrid()
{
 	// 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;

	//m_pComponent_StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));

	m_pComponent_InstancedStaticMesh = NewObject<UInstancedStaticMeshComponent>(this);
	m_pComponent_InstancedStaticMesh->RegisterComponent();
	m_pComponent_InstancedStaticMesh->SetFlags(RF_Transactional);
	this->AddInstanceComponent(m_pComponent_InstancedStaticMesh);
	
}

that static mesh thing was for setting mesh for instanced static mesh component but after i have got some errors i deleted it temporarily.

i found some docs that tells me CreateDefaultSubobject is only for create Subobject(Component) and can use only in constructor, and NewObject is for create independent object so if i want to use it as component i should register it as component manually.

since it sounds like NewObject is much more generally usable, i thought NewObject is better than CreateDefaultSubobject.
but when i compile it, it returns error that says “NewObject with empty name can’t be used to create default subobjects ~~~”

and i can’t find any way to set name for newly created object.
how i set name object?

Hi hhm1573,

You should be using CreateDefaultSubobject in your constructor. The NewObject way should only be done if you need to dynamically add components. That being said, the error you’re encountering is because you’re only providing the Outer parameter in your call to NewObject. The second parameter, which you’re missing, is for the object’s name, so your code would look like this: m_pComponent_InstancedStaticMesh = NewObject<UInstancedStaticMeshComponent>(this, "NameOfTheComponent");

FActorSpawnParameters SpawnParameters;
SpawnParameters.Name = MakeUniqueObjectName(GetWorld(), AMyCharacter::StaticClass());
SpawnParameters.Owner = this;
SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
AMyCharacter* Character = GetWorld()->SpawnActor(SpawnParameters);

OR

FName NameOfBlock = MakeUniqueObjectName(GetWorld(), AMyBlock::StaticClass());

// Construct time:

UChildActorComponent* BlockInWorld = CreateDefaultSubobject<UChildActorComponent>(NameOfBlock);

// Runtime:

UChildActorComponent* BlockInWorld = NewObject<UChildActorComponent>(this, UChildActorComponent::StaticClass());
BlockInWorld->SetChildActorClass(AMyBlock::StaticClass());
BlockInWorld->CreateChildActor();
BlockInWorld->SetupAttachment(RootComponent);
BlockInWorld->SetRelativeLocation(BlockLocation);