Get Color From textue

Hello there!

I have a problem I have a line trace and I need to get the texture color at the hit point

I tried to retrieve the color using this Function

FColor AFullStaticLidar::GetColorFromTexture(UTexture2D* Texture, FVector2D UV, UObject* WorldContextObject)
{
// Ensure the texture is valid
if (!Texture || !Texture->GetPlatformData() || Texture->GetPlatformData()->Mips.Num() == 0)
{
UE_LOG(LogTemp, Warning, TEXT(“Invalid texture or missing platform data.”));
return FColor::Black;
}

// Access the first mip level (full-resolution)
FTexture2DMipMap& Mip = Texture->GetPlatformData()->Mips[0];
int32 TextureWidth = Mip.SizeX;
int32 TextureHeight = Mip.SizeY;

// Calculate pixel position based on UV coordinates
int32 X = FMath::Clamp(static_cast<int32>(UV.X * TextureWidth), 0, TextureWidth - 1);
int32 Y = FMath::Clamp(static_cast<int32>(UV.Y * TextureHeight), 0, TextureHeight - 1);

// Lock texture data for reading
FColor SampleColor = FColor::Black;
void* Data = Mip.BulkData.Lock(LOCK_READ_ONLY);
if (Data)
{
    FColor* FormattedImageData = static_cast<FColor*>(Data);
    SampleColor = FormattedImageData[Y * TextureWidth + X];
}
Mip.BulkData.Unlock();

return SampleColor;

}

and this is the part of the line trace function

UPrimitiveComponent* HitComponent = HitResult.GetComponent();
if (HitComponent)
{
HitMass = HitComponent->GetMass(); // Capture the mass from the component

 // Attempt to get the first material from the hit component
 UMaterialInstanceDynamic* DynMaterial = Cast<UMaterialInstanceDynamic>(HitComponent->GetMaterial(0));
 if (!DynMaterial)
 {
     // If the material is not dynamic, get a static material and create a dynamic instance
     UMaterialInterface* Material = HitComponent->GetMaterial(0);
     if (Material)
     {
         DynMaterial = UMaterialInstanceDynamic::Create(Material, this);
     }
 }

 // Check if we have a valid dynamic material instance
 if (DynMaterial)
 {
     // Create a texture variable of the correct type
     UTexture* Texture = nullptr; 
     // Declare as UTexture* instead of UTexture2D*
     DynMaterial->GetTextureParameterValue(FName("TexturParam"), Texture); // This now expects UTexture*

     // Calculate UV from hit location
     FVector2D UV = CalculateUVFromHitLocation(HitLocation, HitComponent);

     // Get the color from the texture using the calculated UV
     if (Texture) // Ensure the texture is valid before sampling
     {
         // Cast the UTexture* to UTexture2D* to use in GetColorFromTexture if needed
         UTexture2D* Texture2D = Cast<UTexture2D>(Texture);
         if (Texture2D)
         {
             HitColor = GetColorFromTexture(Texture2D, UV, this);
         }
         else
         {
             UE_LOG(LogTemp, Warning, TEXT("Texture is not of type UTexture2D."));
         }
     }
     else
     {
         UE_LOG(LogTemp, Warning, TEXT("No valid UTexture found in the material."));
     }
 }

}

it ways gives me null as the texture is invalid
anyone know how to get the color correctly from the texture I’m using UE 5.4.4
thanks for your time