How can I assign my own dynamic generated texture in C++ to an object in the scene?

the safest way is that you create another UTexture2D pointer and copy your generated texture to it when it is ready.
this way you are sure even if you are working on a separate thread there won’t be any conflict over accessing a locked texture.

after that you need to make a blueprint function that simply return a pointer to UTexture2D you have your texture stored in

I am trying to assign my own generated texture in C++ in an object in the scene in Unreal e.g. a cube.
The code with which I generate the texture is outlined below. I am using data from a Kinect device to create a color texture - just like a normal camera.

// @BeginPlay
    ColorTexture = UTexture2D::CreateTransient(1920, 1080);
    ColorTexture->UpdateResource();
    ColorTexture->SRGB = 1;
    ColorQuad = new RGBQUAD[1920 * 1080];
    // @Tick
    	pColorBuffer = ColorQuad;
    	nColorBufferSize = 1920 * 1080 * sizeof(RGBQUAD);
    	HRESULT hr = ColorFrameReader->AcquireLatestFrame(&ColorFrame); // ColorFrameReader and ColorFrame are from Kinect library.
    	hr = ColorFrame->CopyConvertedFrameDataToArray(nColorBufferSize, reinterpret_cast<BYTE*>(pColorBuffer), ColorImageFormat_Bgra);
    	const size_t SizeColorQuad = 1920 * 1080 * sizeof(RGBQUAD);
    	uint8* Destination = (uint8*)ColorTexture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
    	FMemory::Memcpy(Destination, (uint8*)ColorQuad, SizeColorQuad);
    	ColorTexture->PlatformData->Mips[0].BulkData.Unlock();
    	ColorTexture->UpdateResource();

These are the objects in my scene with which I am experimenting:

https://i.imgur.com/q9IUd5p.png

How can I assign my own dynamic generated texture in C++ to an object in the scene?

Thank you!
I exposed the UTexture2D to blueprint and used it there to set the texture for the material of my object in the scene and it works!