Unreal Editor crashes when adding

The error occurrs in UOpenDoor::BeginPlay() in line 25, please add the function listing to the question.

Added this Code to the .h:

public:
	UPROPERTY(VisibleAnywhere)
	float OpenAngle = 90.0f;
	
	UPROPERTY(EditAnywhere)
	ATriggerVolume* PressurePlate;

	UPROPERTY(EditAnywhere)
	AActor* ActorThatOpens; //Pawn inherits from actor

and this is the .cpp:

 // Fill out your copyright notice in the Description page of Project Settings.
 
 #include "BuildingGame.h"
 #include "OpenDoor.h"
 
 
 // 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;
 
     // ...
 }
 
 
 // Called when the game starts
 void UOpenDoor::BeginPlay()
 {
     Super::BeginPlay();
 
     //////////////////////////////////////////////////////////////////////////
 }
 
 void UOpenDoor::OpenDoor()
 {
     AActor* Owner = GetOwner();
 
     FRotator NewRotation = FRotator(0.0f, -160.0f, 0.0f);
 
     Owner->SetActorRotation(NewRotation);
 }
 
 // Called every frame
 void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
 {
     Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
 
     // ...
 
     if (PressurePlate->IsOverlappingActor(ActorThatOpens))
     {
         OpenDoor();
     } 
 }
 

I did set the pressure Plate to the TriggerVolume and added this as component to the Actor (Door).
My Goal was to be able to set an actor that opens the door (changes yaw) when it is in the trigger volume.

Can anyone help? I get this Error (UE Editor Crashes):

Fatal error!

Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x00000340

UE4Editor-Engine.dll!0x00000000AB8790A1
UE4Editor-BuildingGame-9929.dll!UOpenDoor::TickComponent() [d:\users\lf\documents\repos\unrealcourse\buildinggame\source\buildinggame\opendoor.cpp:42]
UE4Editor-Engine.dll!0x00000000ABC2C05F
UE4Editor-Engine.dll!0x00000000ABC51A15
UE4Editor-Engine.dll!0x00000000AC7DD9DD
UE4Editor-Engine.dll!0x00000000AC7E385D
UE4Editor-Core.dll!0x00000000C09ED84D
UE4Editor-Core.dll!0x00000000C09EDC6D
UE4Editor-Core.dll!0x00000000C0A0CCEB
UE4Editor-Engine.dll!0x00000000AC808861
UE4Editor-Engine.dll!0x00000000AC819532
UE4Editor-Engine.dll!0x00000000AC147B64
UE4Editor-Engine.dll!0x00000000AC1501AD
UE4Editor-Engine.dll!0x00000000ABF05B17
UE4Editor.exe!0x000000007A96DEE3
UE4Editor.exe!0x000000007A95EB40
UE4Editor.exe!0x000000007A95EBBA
UE4Editor.exe!0x000000007A970A29
UE4Editor.exe!0x000000007A9722B6
KERNEL32.DLL!0x00000000E8692774
ntdll.dll!0x00000000E8F90D61
ntdll.dll!0x00000000E8F90D61

EDIT: Changed Error Log Output

Sorry had pasted an older error :/, already found out its in the wroing place and moved it to the part which is executed every Frame.

I changed it in the original post

The Code above is all I changed, but here is the whole .cpp file

:
 // Fill out your copyright notice in the Description page of Project Settings.
 
 #include "BuildingGame.h"
 #include "OpenDoor.h"
 
 
 // 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;
 
     // ...
 }
 
 
 // Called when the game starts
 void UOpenDoor::BeginPlay()
 {
     Super::BeginPlay();
 
     //////////////////////////////////////////////////////////////////////////
 }
 
 void UOpenDoor::OpenDoor()
 {
     AActor* Owner = GetOwner();
 
     FRotator NewRotation = FRotator(0.0f, -160.0f, 0.0f);
 
     Owner->SetActorRotation(NewRotation);
 }
 
 // Called every frame
 void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
 {
     Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
 
     // ...
 
     if (PressurePlate->IsOverlappingActor(ActorThatOpens))
     {
         OpenDoor();
     } 
 }

Most probably you PreasurePlate is not (yet) initialized.

  • Either you forgot to initialize it at all (it is not in the code listing in the question),
  • or it is not yet there in the first few ticks (if you initialize it from Blueprint at some time)

If it is the first you need something like this in the constructor:

PressurePlate = CreateDefaultSubobject<ATriggerVolume>(TEXT("PressurePlate"));

if its the latter, you could change your code:

      if (PressurePlate && PressurePlate->IsOverlappingActor(ActorThatOpens))
      {
          OpenDoor();
      }

Thanks, adding the “PressurePlate &&” to the If Statement, fixed it. Could you explain what was wrong? I followed a tutorial, and he didnt said anything about this.

I can only speculate on the cause of the problem. My code fix only addresses the symptom, which is that PressurePlate was nullptr at the time is was accessed (the if (PressurePlate) is similar to if ((PressurePlate != nullptr)).

Because the access occurs in Tick, it could be that it simply has been initialized later than the tick function begins to tick. If this is the case, the proposed fix is fine, although it would be prettier to ensure it to be valid on first tick.

Its also possible that PressurePlate is never initialized. You will know it if OpenDoor() is never called at all.

I know this was answered. But i want you to know. The first Thing is the Problem. The Pressure plate was not initaliziesed.

I had the same Problem. The Course for learning c++ in unreal was made with 4.10. Now you have to find some Solutions. BUT: I think this a good practise. Beacuse you learn a lot more.

Cheers