Scenario: I have an actor with an audio component (set up with the audio to be played), and an actor component called . In the TriggerExplosion.h, I have it set up as
UPROPERTY(EditAnywhere) UAudioComponent* MyAudio = nullptr;
and in the cpp, I try to call it in the BeginPlay section, defining it using
MyAudio = GetOwner()->FindComponentByClass<UAudioComponent>();
In the PlayTrigger() function, Im trying to call it using “MyAudio->Play();”
, but the audio set doesnt play… Here is the cpp script. Also, the header has the required Include.
#include "TriggerExplosion.h"
// Sets default values for this component's properties
UTriggerExplosion::UTriggerExplosion()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
}
// Called when the game starts
void UTriggerExplosion::BeginPlay()
{
Super::BeginPlay();
MyAudio = GetOwner()->FindComponentByClass<UAudioComponent>();
CheckTrigger();
CheckAudio();
PlayTrigger();
}
// Called every frame
void UTriggerExplosion::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
}
void UTriggerExplosion::CheckTrigger()
{
if (!TriggerFX)
{
UE_LOG(LogTemp, Error, TEXT("MISSING: TRIGGER VOLUME"));
}
else
{
UE_LOG(LogTemp, Warning, TEXT("FOUND: TRIGGER VOLUME"));
}
}
void UTriggerExplosion::CheckAudio()
{
if (!MyAudio)
{
UE_LOG(LogTemp, Error, TEXT("MISSING: AUDIO COMPONENT"));
}
else
{
UE_LOG(LogTemp, Warning, TEXT("FOUND: AUDIO COMPONENT"));
}
}
void UTriggerExplosion::PlayTrigger()
{
if (MyAudio)
{
MyAudio->Play();
}
else
{
return;
}
}