Unreal Crash on play

Hi, I’m following the lecture and for some reason when I hit the play button the app crashed.

I started debugging and realized it crashes on this line :

	AudioComponent = GetOwner()->FindComponentByClass<UAudioComponent>();

when I comment it everything works but with no sound.

This is my Code

void UOpenDoor::FindAudioComponent()
{
		AudioComponent = GetOwner()->FindComponentByClass<UAudioComponent>();
		if(!AudioComponent){
			UE_LOG(LogTemp, Error , TEXT("%s has the OpenDoor has but do not have audio component set!"), *GetOwner()->GetName());	
	}

}

I have the #include “Components/AudioComponent.h” at the top of the .cpp file.

Losing my mind here.
Thanks,

Where do you call that function?

I call this function here:

void UOpenDoor::BeginPlay()
{
	Super::BeginPlay();

	InitialYaw = GetOwner()->GetActorRotation().Yaw;
	CurrentYaw = InitialYaw;
	OpenAngle += InitialYaw; 
	FindAudioComponent();	
	FindpressurePlate();
}

Strange, any chance you’re also calling it in the constructor by mistake?

nope,
Here is my entire code maybe it will shed some light…

// Fill out your copyright notice in the Description page of Project Settings.


#include "OpenDoor.h"
#include "Engine/World.h"
#include "Components/PrimitiveComponent.h"
#include "Components/AudioComponent.h"
#include "GameFramework/PlayerController.h"
#include "GameFramework/Actor.h"

#define OUT

// Sets default values for this component's properties
UOpenDoor::UOpenDoor()
{
	// 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;
}

void UOpenDoor::FindAudioComponent()
{
		AudioComponent = GetOwner()->FindComponentByClass<UAudioComponent>();
		if(!AudioComponent){
			UE_LOG(LogTemp, Error , TEXT("%s has the OpenDoor has no audio component set!"), *GetOwner()->GetName());	
	}

}

void UOpenDoor::FindpressurePlate()
{
	if(!PressurePlate)
		{
			UE_LOG(LogTemp, Error, TEXT("%s has the open door component on it, but no pressureplate set!"), *GetOwner()->GetName());	
		}
}

// Called when the game starts
void UOpenDoor::BeginPlay()
{
	Super::BeginPlay();

	InitialYaw = GetOwner()->GetActorRotation().Yaw;
	CurrentYaw = InitialYaw;
	OpenAngle += InitialYaw; 
	FindAudioComponent();	
	FindpressurePlate();
}


// Called every frame
void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);	
	//Adding the PressurePlate in order to make sure the pointer is not NULL;
	if (PressurePlate && TotalMassOfActors() > MassToOpenDoor)
	{
		OpenDoor(DeltaTime);
		DoorOpenTime = GetWorld()->GetTimeSeconds();
	}
	else if( GetWorld()->GetTimeSeconds() - DoorOpenTime > DoorCloseDelay)
	{
		CloseDoor(DeltaTime);
	}

}

float UOpenDoor::TotalMassOfActors() const
{

	float TotalMass = 0.f;
	
	//find ovelapping actors and accumulate mass

	TArray<AActor*> OverlappingActors;
	if (!PressurePlate) {return TotalMass;}
	PressurePlate->GetOverlappingActors(OUT OverlappingActors);

	for (AActor* Actor : OverlappingActors) 
	{
		  TotalMass += Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();
	}

	return TotalMass;

}

void UOpenDoor::OpenDoor(float DeltaTime)
{
	CurrentYaw = FMath::Lerp(CurrentYaw, OpenAngle, DeltaTime * DoorOpenSpped);	

	FRotator DoorRotation = GetOwner()->GetActorRotation();
	DoorRotation.Yaw = CurrentYaw;

	GetOwner()->SetActorRotation(DoorRotation);
	if(!AudioComponent) {return;}
		UE_LOG(LogTemp, Warning , TEXT("will play sound on Open %s"), bPlaySound)

	if(bPlaySound)
	{
		// AudioComponent->Play();
		bPlaySound = false;
	}
}

void UOpenDoor::CloseDoor(float DeltaTime)
{
	CurrentYaw = FMath::Lerp(CurrentYaw, InitialYaw, DeltaTime * DoorCloseSpeed);	

	FRotator DoorRotation = GetOwner()->GetActorRotation();
	DoorRotation.Yaw = CurrentYaw;

	GetOwner()->SetActorRotation(DoorRotation);
	if(!AudioComponent) {return;}
	UE_LOG(LogTemp, Warning , TEXT("will play sound on Close %s"), !bPlaySound)

	if(!bPlaySound)
	{
		// AudioComponent->Play();
		bPlaySound = true;
	}
}


And you’re sure it’s this line that causes it?

AudioComponent = GetOwner()->FindComponentByClass<UAudioComponent>();

Privacy & Terms