# What is the best practice for storing a reference to a component belonging to another actor?

**URL:** <https://forums.unrealengine.com/t/what-is-the-best-practice-for-storing-a-reference-to-a-component-belonging-to-another-actor/468830>\
**Category:** Editor Scripting\
**Tags:** garbage-collector, reference, unreal-engine, question\
**Created:** [July 29, 2020, 12:10am UTC](https://forums.unrealengine.com/t/what-is-the-best-practice-for-storing-a-reference-to-a-component-belonging-to-another-actor/468830 "2020-07-29T00:10:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![clockwise\_mu](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@clockwise\_mu](https://forums.unrealengine.com/u/clockwise_mu)\
**Post date:** [July 29, 2020, 12:10am UTC](https://forums.unrealengine.com/t/what-is-the-best-practice-for-storing-a-reference-to-a-component-belonging-to-another-actor/468830/1 "2020-07-29T00:10:20Z")

</div>

What is the best practice for referencing a component to another actor?

I am storing references to components that belong to other actors within certain actors. I am wondering what the suggested way to implement this, in terms of both member declaration and definition.

For example in my custom actor class, I can declare this reference in different ways:

```
USomeComponent* otherActorsComponent;

```

or

```
UPROPERTY(Transient)
USomeComponent* otherActorsComponent;

```

And I can assign an initial value to this pointer like:

```
void BeginPlay() {
   // .....
    otherActorsComponent = otherActor->FindComponentByClass<USomeComponent>();
}

```

I ask because I’m having frequent issues of what appears to be the garbage collector causing sporadic crashes on Start, during, and at Stop of PIE. I’m not having issues that directly implicate any of these references, but I’m wondering if I’m messing up my GC object hierarchy somehow.

---

<div class="post-metadata">

**Author:** ![knapeczadam](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@knapeczadam](https://forums.unrealengine.com/u/knapeczadam)\
**Post date:** [July 29, 2020, 4:56pm UTC](https://forums.unrealengine.com/t/what-is-the-best-practice-for-storing-a-reference-to-a-component-belonging-to-another-actor/468830/2 "2020-07-29T16:56:09Z")

</div>

> **[Unreal Object Handling](https://docs.unrealengine.com/5.3/en-US/unreal-object-handling-in-unreal-engine/)**
>
> Overview of the features of the UObject system.

Automatic Updating of References

> An Object reference stored in a raw  
> pointer will be unknown to the Unreal  
> Engine, and will not be automatically  
> nulled, nor will it prevent garbage  
> collection. Note this does not mean  
> that all UObject\* variables must be  
> UProperties. If you want an Object  
> pointer that is not a UProperty,  
> consider using TWeakObjectPtr.

Garbage Collection

> One practical implication here is that  
> you typically need to maintain a  
> UPROPERTY reference to any Object you  
> wish to keep alive, or store a pointer  
> to it in a TArray or other Unreal  
> Engine container class.

As a rule of thumb: use `UPROPERTY` macro when you are using Actor or component references as a member variable.
