How to connect an actor with pawn to access the Pawn variables?

Hello guys! I have a problems with variables of a Pawn. I have a Pawn where i change different variables and inputs (like a bool variable of left click mouse). I have a Static Mesh actor. I want to interact with actor using variables from my Pawn. I tried to cast it with my pawn, but when i trying to get access to my variable my project crashes. (Also i know that in blueprint cast node is easy to use. In blueprint we can make a actor reference variable. but in C++ there is nothing like that I think).

#include "BuildYourOwnClassC.h"
#include "ObjectTemplate.h"
#include "CursorPawn.h"
// Sets default values
AObjectTemplate::AObjectTemplate()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	MyMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyMesh"));
}

// Called when the game starts or when spawned
void AObjectTemplate::BeginPlay()
{
	Super::BeginPlay();
ACursorPawn* MyPC = Cast<ACursorPawn>(UGameplayStatics::GetPlayerController(GetWorld(), 0));
	if (MyPC->bRMB) {
	UE_LOG(LogTemp, Warning, TEXT("IT WORKS! (of course it doesn't:( )"))
	}
	
	
}

// Called every frame
void AObjectTemplate::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

Help pls! How can I connect my CursorPawn with this actor??

ACursorPawn is derived from APlayerController?

GetPlayerController doesn’t retrieve a Pawn, it retrieves a Player Controller (PC); is your Pawn a Pawn, or is it a Controller? These are different things. The PC receives input from the player and controls the behavior of a Pawn (which can also be controlled by other Controllers, like an AI Controller).

If you want to retrieve a Pawn, you can get it from the PC:

APlayerController* MyPC = UGameplayStatics::GetPlayerController(GetWorld(), 0);

//always check that the pointer is correctly assigned before using it
if (MyPC)
{
    ACursorPawn* MyPawn = Cast<ACursorPawn>(MyPC->GetPawn());
    if (MyPawn && MyPawn->bRMB)
    {
        UE_LOG(LogTemp, Warning, TEXT("IT WORKS NOW!"));

     }

}

“(Also i know that in blueprint cast node is easy to use. In blueprint we can make a actor reference variable. but in C++ there is nothing like that I think).”

Everything in BP is possible in C++; Blueprint is written in C++. There are things that can be done in C++ that can’t be done in BP, but not the other way around.

Thanks for answering! It helped me! A created a variable ACursorPawn, and casted to my Pawn in BeginPlay. Now it works! (Of Course before it I got a few crashes. but it works now.) thanks a lot, dude. May the Force be with you!