Assertian failed during SetStaticMesh call

While calling SetStaticMesh in my C++ code I receive assertions like this for several of my fbx files:

Engine\Source\Runtime\Core\Public\Containers\Array.h(536): Assertion failed: (Index >= 0) & (Index < ArrayNum)
Array index out of bounds: 8 from an array of size 5

In my use case I need to import the fbx file at runtime without combining its meshes. They are about 1000 meshes, not 5 as the assertion suggests. This fbx file is already imported in the Editor, all 1000 static meshes are shown in the Content Browser and by selecting them all and drag&drop them into the scene I can see the model.

I want to reach the same effect when loading the assets from C++ code.

Import options I used:

15205-importoptionsstaticmesh.jpg

The code snippet I use for setting a staticMesh:

ConstructorHelpers::FObjectFinder staticMesh(*assetName); m_meshes.getComponents().Add(PCIP.CreateAbstractDefaultSubobject(this, *assetName)); m_meshes.getComponentAtPos(i)->SetStaticMesh(staticMesh.Object);

Where are you doing this line:

ConstructorHelpers::FObjectFinder staticMesh(*assetName); 

It will need to be in the constructor which I’m assuming your doing as your also using the PCIP. Also, “8 from an array of size 5” may be due to the “getComponentAtPos(i)”, what can your ‘i’ variable go up to, it may be that it is trying to access a component that doesn’t exist.

Yes, the code is inside the constructor of a subclass of AActor. The i variable can go from 0 to the total size of the meshes inside the fbx file which are hundreds.
Inside the the class I have a class member variable m_meshes of custom type FSubObjStruct which allows me to manage subobjects in a TArray:



USTRUCT()
struct FSubObjStruct
{
    GENERATED_USTRUCT_BODY()

    UPROPERTY(VisibleAnyWhere, Category = "My Component")
    TArray<TSubobjectPtr> m_components;

    int32 getCount() const
    {
        return m_components.Num();
    }

    TSubobjectPtr getComponentAtPos(int32 idx)
    {
        return m_components[idx];
    }

    TArray<TSubobjectPtr>& getComponents()
    {
        return m_components;
    }

    FSubObjStruct()
    {
    }
};

This was the problem. The mesh names contain numbers but they are not named consecutively which caused my indexing into the array to be wrong.