# Component instance reference in Unreal Engine 4

I'm creating a C++ script and I want to get a reference to a component instance.

For example, imagine that I want to get the player camera, which is a UCameraComponent. I would create a UPROPERTY like this:

 UPROPERTY(EditAnywhere, meta = (AllowPrivateAccess = "true"))
class UCameraComponent* PlayerCamera;


However, I can't assign it in the editor details panel like I would in Unity. I would expect to be able to drag and drop a Component from an Actor on the World Outline to this property but it seems that's not possible. Moreover, when clicking on the dropdown in the details panel, I don't have any available to choose from.

From what I learned, it just isn't possible to do it this way and the Unreal way is to create a UPROPERTY to store a reference to the Actor and then get the component in runtime. Although this might work for most cases, it's still a bit memory inefficient since you might not need the Actor reference after getting the Component (yes, I know it's just a pointer, but still...). Plus, how would you handle multiple components of the same class in the same actor? I assume you have to loop over them and try to find the one you want, based on a priori knowledge of their order in the hierarchy or based on some of their known properties (maybe with tags)...

All of this seems kind of messy, so I ask you to please shed some light on what's the (best) Unreal workflow for setting component references.

To do this in Unreal C++, inside the CPP file you need to do the following:

Make sure to include the header component:

#include "Camera/CameraComponent.h"

Then in the class constructor:

CameraComponent = CreateDefaultSubobject <UCameraComponent>("CameraComponent");

From here you need to set up the attachment which is usually to the mesh if it's a character but can be attached to anything.

CameraComponent->SetUpAttachment(GetMesh());