I need to create my actorcomponent into a character.
Tried through NewObject, but then everything crashes.
UMyActorComponent* MyComponent = NewObject<UActorComponent> (this, UActorComponent :: StaticClass (), TEXT (“MyCompoent”));
if (MyComponent)
{
MyComponent-> RegisterComponent ();
}
How can I attach ActorComponent to a character in C ++?
Kris
(Kris)
2
UActorComponent is abstract - you should be using your components class.
Try some other component first:
USphereComponent* SphereComponent = NewObject<USphereComponent>(this);
if (SphereComponent != nullptr)
{
SphereComponent->SetSphereRadius(10.0f, false);
SphereComponent->SetCollisionProfileName(UCollisionProfile::NoCollision_ProfileName);
SphereComponent->AttachToComponent(this, FAttachmentTransformRules::SnapToTargetIncludingScale);
SphereComponent->RegisterComponent();
SphereComponent->SetVisibility(true);
SphereComponent->SetHiddenInGame(false);
}
Also, where do you want to create this component?
If you’re creating this in the constructor, you use CreateOptionalDefaultSubobject:
USphereComponent* SphereComponent = ObjectInitializer.CreateOptionalDefaultSubobject<USphereComponent>(this);
if (SphereComponent != nullptr)
{
SphereComponent->SetSphereRadius(10.0f, false);
SphereComponent->SetCollisionProfileName(UCollisionProfile::NoCollision_ProfileName);
SphereComponent->SetupAttachment(RootComponent);
SphereComponent->SetVisibility(true);
SphereComponent->SetHiddenInGame(false);
}
Thanks for your reply. Yes, I wanted to create it in the constructor. The second method worked.