LoadObject can't create a Texture2D from path (path is correct!)

I’m trying to create a Texture2D from a file path in my C++ file, and I’m receiving errors that it cannot do so.

The code:

        FString initialBackgroundPath =
            FPaths::ProjectContentDir() + "Core/screens/" + FString::FromInt(screenIndex) + "/main/background_0.png";
        UE_LOG(LogTemp, Log, TEXT("File path: %s"), *initialBackgroundPath);
        UTexture2D* initialBackgroundTexture = LoadObject<UTexture2D>(nullptr, *initialBackgroundPath);

This gives me the error:

LogUObjectGlobals: Warning: Failed to find object 'Texture2D None.D:/MyProject/Content/Core/screens/1/main/background_0.png'

I went ahead and checked in my File Explorer that this file exists, and the path is 100% correct – given that the path is correct, I’m actually kind of lost as to what else could be wrong here! It’s a pretty simple function call but the error is surprisingly mysteryious.

Any suggestions for what else could be wrong or how else to troubleshoot?

Hi scottnla1,

The LoadObject and FindObject are for loading “uasset” files, the native UE files.

Check out this thread about loading pngs:

How should I be using LoadObject if I have the correct uasset?

I actually tried creating a .uasset:

image

And changing the C++ code to reference the .uasset file instead:

        FString initialBackgroundPath =
            FPaths::ProjectContentDir() + "Core/screens/" + FString::FromInt(screenIndex) + "/main/T_background_0.uasset";
        UE_LOG(LogTemp, Log, TEXT("File path: %s"), *initialBackgroundPath);
        UTexture2D* initialBackgroundTexture = LoadObject<UTexture2D>(nullptr, *initialBackgroundPath);

But this gives me the same error:

LogUObjectGlobals: Warning: Failed to find object 'Texture2D None.D:/MyProject/Content/Core/screens/1/main/T_background_0.uasset'

You’re very close, just change the filename part to “T_background_0.T_background_0” - confusing I know, but the first one is for the “Package” (the uasset file) and the second one is for the asset contained in the package…

I tried that and still get errors:

LogUObjectGlobals: Warning: LoadPackage can't find package Game/Core/screens/1/main/T_background_0.
LogUObjectGlobals: Warning: LoadPackage can't find package Game/Core/screens/1/main/T_background_0.
LogUObjectGlobals: Warning: Failed to find object 'Texture2D Game/Core/screens/1/main/T_background_0.T_background_0'

I created the assets by dragging and dropping them into the Content Drawer – could that be why maybe they aren’t structured properly?

Dragging them in should be ok as long as there’s no import errors.

You won’t need the “ProjectContentDir()” - replace that with “/Game/” and it should work.

That was it! Thank you so much!

1 Like