// Exact error
‘Template Mismatch during attachment. Attaching instanced component to template component.’
This is trying to attach a SphereCollision component to a Mesh Component
I have a projectile class. When trying to spawn it, I get the above error when running in debug. If I don’t run debug, it just spawns at the origin (0,0,0) and calling SetActorLocation doesn’t even affect it.
// The constructor for my projectile where I initialize it's components
AProjectile::AProjectile()
{
ProjectileComponent = CreateDefaultSubobject<UProjectileComponent>("ProjectileComponent");
SphereCollision = CreateDefaultSubobject<USphereComponent>("SphereCollision");
if (SphereCollision)
{
RootComponent = SphereCollision;
SphereCollision->SetSphereRadius(16, false);
SphereCollision->AttachParent = nullptr;
}
if (ProjectileComponent)
{
ProjectileComponent->UpdatedComponent = SphereCollision;
}
}
// Spawning a new projectile
Projectile = GetWorld()->SpawnActor<AProjectile>(AProjectile::StaticClass());
// Caster is an 'ACharacter'
Projectile->AttachRootComponentTo(Caster->GetMesh(), "Spell_Charge_Center", EAttachLocation::Type::SnapToTarget);
I tried calling SetTags() to set the tags to be the same as the MeshComponent’s tags. But that caused a different exception. What do I need to do in order to be able to attach this projectile to my Mesh? I know it can be done, because I have it working in Blueprints, I’m just trying to replicate it in C++
UPDATE: ProjectileComponent is a MovementComponent. I don’t think that is relevant in this case, but just in case…
Here is the header for Projectile…
UCLASS()
class GAME_API AProjectile : public AActor
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Projectile")
UProjectileComponent* ProjectileComponent;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Projectile")
USphereComponent* SphereCollision;
};