# How to set reference pointer

**URL:** <https://forums.unrealengine.com/t/how-to-set-reference-pointer/123880>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [March 22, 2019, 7:17am UTC](https://forums.unrealengine.com/t/how-to-set-reference-pointer/123880 "2019-03-22T07:17:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_ae8cc15b](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ae8cc15b](https://forums.unrealengine.com/u/anonymous_user_ae8cc15b)\
**Post date:** [March 22, 2019, 7:17am UTC](https://forums.unrealengine.com/t/how-to-set-reference-pointer/123880/1 "2019-03-22T07:17:21Z")

</div>

I’m UE4 and C ++ beginner.  
I want to refer to variables of other classes, but when UE4 is played, I got a error that the corresponding reference pointer is null.  
In the program, certainly it looks like there is nothing set in the pointer to refer to variable of another class.  
So I want to write code in the constructor to set it to a pointer to refer to.  
I describe the source file to be referred to below.

The referred side is simply described as a normal member variable.  
UPROPERTY (EditAnywhere, category = “Countdown”)  
int32 CountdownTime;

Reference side  
AMyActor :: Tick (float DeltaTime)  
if (Countdown-\> CountdownTime \<1) {  
;  
}  
I refer to member variables of other classes, but I get error that Countdown is null.  
Certainly Countdown has not been set, so I’m trying to set it in the constructor.

Please let me tell.

MyActor.h  
UCLASS()  
class HOWTO\_VTE\_API AMyActor : public AActor  
{  
GENERATED\_BODY()  
public:  
// Sets default values for this actor’s properties  
AMyActor();  
protected:  
// Called when the game starts or when spawned  
virtual void BeginPlay() override;  
public:  
// Called every frame  
virtual void Tick(float DeltaTime) override;

UPROPERTY(EditAnywhere)  
class ACountdown\* Countdown;  
};

MyActor.cpp  
#include “MyActor.h”  
#include “Countdown.h”

// Sets default values  
AMyActor::AMyActor()  
{

PrimaryActorTick.bCanEverTick = true;  
// Countdown = CreateDefaultSubobject\<ACountdown\>(“Countdown”);  
// Countdown = (ACountdown\*)GetDefaultSubobjectByName(“Countdown7”);  
}

// Called when the game starts or when spawned  
void AMyActor::BeginPlay()  
{  
Super::BeginPlay();  
}

// Called every frame  
void AMyActor::Tick(float DeltaTime)  
{

// Super::Tick(DeltaTime);  
if (Countdown-\>CountdownTime \< 1) {  
;  
}  
}

Thanks.

---

<div class="post-metadata">

**Author:** ![Phantas](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Phantas](https://forums.unrealengine.com/u/Phantas)\
**Post date:** [March 22, 2019, 5:09pm UTC](https://forums.unrealengine.com/t/how-to-set-reference-pointer/123880/2 "2019-03-22T17:09:07Z")

</div>

> [@anonymous\_user\_ae8cc15b](#):
>
> I’m UE4 and C ++ beginner.  
> I want to refer to variables of other classes, but when UE4 is played, I got a error that the corresponding reference pointer is null.  
> In the program, certainly it looks like there is nothing set in the pointer to refer to variable of another class.  
> So I want to write code in the constructor to set it to a pointer to refer to.  
> I describe the source file to be referred to below.
> 
> The referred side is simply described as a normal member variable.
> 
> ```auto
> 
> UPROPERTY (EditAnywhere, category = "Countdown")
> int32 CountdownTime;
> 
> ```
> 
> Reference side
> 
> ```auto
> 
> void AMyActor::Tick (float DeltaTime)
> {
> if (Countdown->CountdownTime <1)
> {
> ;    
> }
> }
> 
> ```
> 
> I refer to member variables of other classes, but I get error that Countdown is null.  
> Certainly Countdown has not been set, so I’m trying to set it in the constructor.
> 
> Please let me tell.
> 
> MyActor.h
> 
> ```auto
> 
> UCLASS()
> class HOWTO_VTE_API AMyActor : public AActor
> {
> GENERATED_BODY()
> 
> public:
> // Sets default values for this actor's properties
> AMyActor();
> 
> protected:
> // Called when the game starts or when spawned
> virtual void BeginPlay() override;
> 
> public:
> // Called every frame
> virtual void Tick(float DeltaTime) override;
> 
> UPROPERTY(EditAnywhere)
> class ACountdown* Countdown;
> };
> 
> ```
> 
> MyActor.cpp
> 
> ```auto
> 
> #include "MyActor.h"
> #include "Countdown.h"
> 
> // Sets default values
> AMyActor::AMyActor()
> {
> PrimaryActorTick.bCanEverTick = true;
> // Countdown = CreateDefaultSubobject<ACountdown>("Countdown");
> // Countdown = (ACountdown*)GetDefaultSubobjectByName("Countdown7");
> }
> 
> // Called when the game starts or when spawned
> void AMyActor::BeginPlay()
> {
> Super::BeginPlay();
> }
> 
> // Called every frame
> void AMyActor::Tick(float DeltaTime)
> {
> Super::Tick(DeltaTime);
> 
> if (Countdown->CountdownTime < 1)
> {
> ;
> }
> }
> 
> ```
> 
> Thanks.

I thought i had an answer to your problem, but it was just some BS… i just think that these two sentences in red in your MyActor.cpp file will bear no result… so Sorry. 🙂

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_ae8cc15b](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ae8cc15b](https://forums.unrealengine.com/u/anonymous_user_ae8cc15b)\
**Post date:** [March 24, 2019, 11:23pm UTC](https://forums.unrealengine.com/t/how-to-set-reference-pointer/123880/3 "2019-03-24T23:23:44Z")

</div>

Thank you for your replay and your edit.  
I do’nt know how to edit for code that like your edit.  
Tell me how to edit for.

Anyway,I have a ploblem yet that have to solve reference pointer.  
Somebody, give me for tips please.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_ae8cc15b](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ae8cc15b](https://forums.unrealengine.com/u/anonymous_user_ae8cc15b)\
**Post date:** [March 25, 2019, 12:34am UTC](https://forums.unrealengine.com/t/how-to-set-reference-pointer/123880/4 "2019-03-25T00:34:16Z")

</div>

I think Countdown instance is made by drag & drop into lebel editor screen.  
I want to get that Countdown instance pointer.

The following line will create a new instance of the Acountdown class.  
// Countdown = NewObject\<ACountdown\>();

Not so, I want a pointer to the object instantiated by UE4 now.  
Is there function to get instance name from class name.  
And, I get pointer from an instance name.  
But I do’nt know such a function.

Thanks.
