ViewportRHI not working

Hello everyone. I have been working on a new project for a few days and I need to get a frame directly from the backbuffer. From what I have been researching, this can be done with RHI by simply using the viewport.

The problem I have arises in the following lines of code:
FViewport* Viewport = GEngine->GameViewport->Viewport;
TRefCountPtr MyViewportRHI = Viewport->GetViewportRHI();

More specifically, the second line of code does not get the ViewportRHI since it always returns null. More specifically, I am using these lines in a function that is called every 60 frames from the Tick function. I am making sure that the code is running in the main thread of the game and that it is all initialized correctly.

Can anyone tell me what is going on? I don’t understand why the ViewportRHI is always null.