Asign TextureRenderTarget2D to SceneCaptureComponent crash


I’m trying to create and asign a UTextureRenderTarget2D to a USceneCaptureComponent2D.

But it cause a crash:

ACapture::ACapture(const FObjectInitializer& ObjectInitializer)
	USceneCaptureComponent2D *CaptureComponent = ObjectInitializer.CreateDefaultSubobject<USceneCaptureComponent2D>(this, TEXT("Capture Component"));

	UTextureRenderTarget2D *RenderTarget = ObjectInitializer.CreateDefaultSubobject<UTextureRenderTarget2D>(this, TEXT("Render Target"));
	if (CaptureComponent != nullptr)
		CaptureComponent->TextureTarget = RenderTarget;

	USphereComponent *sphere = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("Sphere"));
	RootComponent = sphere;

Where am i wrong ?

Thanks in advance for you help.

Worked fine, thanks !

How did you find out ? I’m trying the much i can to search by myself, but for some things there is a big lack of documentation (how do i know that for this particular thing i need the Kismet library ?).
What’s the trick ? :slight_smile:

Try this in BeginPlay and replacing WIDTH and HEIGHT with your desired width and height.

if (CaptureComponent != nullptr)
         CaptureComponent->TextureTarget = UKismetRenderingLibrary::CreateRenderTarget2D(GetWorld(), WIDTH, HEIGHT);

Can’t remember what I looked for when I needed this but what came up in the UE4 docs was a Blueprint node. I just looked up the name of that Blueprint node in the source and found the UKismetRenderingLibrary.