# How to create an array of sub objects?

**URL:** <https://forums.unrealengine.com/t/how-to-create-an-array-of-sub-objects/394603>\
**Category:** C++\
**Tags:** object-picker, tarray:-arrays-in-un, unreal-engine, CPP, question\
**Created:** [May 25, 2017, 7:48pm UTC](https://forums.unrealengine.com/t/how-to-create-an-array-of-sub-objects/394603 "2017-05-25T19:48:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_567aa983](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_567aa983](https://forums.unrealengine.com/u/anonymous_user_567aa983)\
**Post date:** [May 25, 2017, 7:48pm UTC](https://forums.unrealengine.com/t/how-to-create-an-array-of-sub-objects/394603/1 "2017-05-25T19:48:24Z")

</div>

As in topic, I know how to create a sub object for an object:

UPROPERTY( EditAnywhere, Instanced )  
MyObject\* m\_obj;

and in constructor:

m\_obj = ObjectInitializer.CreateDefaultSubobject\< MyObject \>( this, TEXT( “OBJ” ) );

I think this is correct ( ? )

But I have no idea how to create an array of objects that user can expand in editor and add new objects and fill their data, so I would like to have something like:

UPROPERTY( EditAnywhere, Instanced )  
TArray\< MyObj\* \> m\_obj;

But then, how do I call the CreateDefaultSubObject? Is there any way to do it?

---

<div class="post-metadata">

**Author:** ![ExtraLifeMatt](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/extralifematt/32/918695_2.png) [@ExtraLifeMatt](https://forums.unrealengine.com/u/ExtraLifeMatt)\
**Post date:** [May 26, 2017, 2:55pm UTC](https://forums.unrealengine.com/t/how-to-create-an-array-of-sub-objects/394603/2 "2017-05-26T14:55:20Z")

</div>

You need to mark your MyObj as EditInlineNew in the UCLASS/USTRUCT declaration if you want them to be able to instantiate the object within the editor property panel. You already have the Instanced flag set on your property, so that should be everything.

If you are expecting the user to create/fill those objects, then you don’t have to do anything. If you want to manually fill that data, then it’s the exact same process, just with adding it to the array at the end:

```
MyObj* ANewObject = ObjectInitializer.CreateDefaultSubobject< MyObject >( this, TEXT( "SubObjectA" ) );
m_obj.Add(ANewObject);

// Rinse repeat for however many objects you want to add.
```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_567aa983](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_567aa983](https://forums.unrealengine.com/u/anonymous_user_567aa983)\
**Post date:** [May 27, 2017, 3:12pm UTC](https://forums.unrealengine.com/t/how-to-create-an-array-of-sub-objects/394603/3 "2017-05-27T15:12:27Z")

</div>

**EditInlineNew** is exactly what I needed, thanks.

Out of curiosity for the future - you say that I can create the object manually and append at the end of the array. Is there any callback from editor, that I get, when somebody presses “+” in an array - so that I can manually spawn a new object at the end of array? Otherwise, I don’t see a way to do this without writing custom editor I guess ☹

---

<div class="post-metadata">

**Author:** ![ExtraLifeMatt](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/extralifematt/32/918695_2.png) [@ExtraLifeMatt](https://forums.unrealengine.com/u/ExtraLifeMatt)\
**Post date:** [May 27, 2017, 3:44pm UTC](https://forums.unrealengine.com/t/how-to-create-an-array-of-sub-objects/394603/4 "2017-05-27T15:44:24Z")

</div>

Yes. See this [post](https://answers.unrealengine.com/questions/66815/how-to-handle-property-updates-in-code.html).

---

<div class="post-metadata">

**Author:** ![creplogle1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@creplogle1](https://forums.unrealengine.com/u/creplogle1)\
**Post date:** [July 29, 2023, 8:35pm UTC](https://forums.unrealengine.com/t/how-to-create-an-array-of-sub-objects/394603/5 "2023-07-29T20:35:38Z")

</div>

I realize this is a really old post, but hoping for some help on this same scenario in UE5. I’m trying to instantiate an array of USplineComponents within BeginPlay(). In my Actor’s .h I declare

```auto
public:

    UPROPERTY(Instanced)
    TArray<USplineComponent*> Splines;

```

In the Actor’s .cpp

```auto
void AMyActor::BeginPlay()
{
    ...
    USplineComponent* spline = CreateDefaultSubobject<USplineComponent>(TEXT("SomeSpline"), false); // tried true here as well
}

```

When I PIE, UE5 crashes in CoreUObject.dll. I’ve tried subclassing USplineComponent with

```auto
UCLASS(EditInlineNew)
class KUIPER_API UInstancedSplineComponent : public USplineComponent
{
	GENERATED_BODY()
	
};

```

but it still crashes. I don’t need the spline components exposed in the editor, I just need to generate them dynamically during gameplay.

**EDIT:**  
I was able to fix this by using

```auto
USplineComponent* spline = NewObject<USplineComponent>(this, TEXT("SomeSpline"));

```

instead of `CreateDefaultSubobject`

---

<div class="post-metadata">

**Author:** ![UnrealEverything](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@UnrealEverything](https://forums.unrealengine.com/u/UnrealEverything)\
**Post date:** [July 31, 2023, 6:13am UTC](https://forums.unrealengine.com/t/how-to-create-an-array-of-sub-objects/394603/6 "2023-07-31T06:13:24Z")

</div>

`CreateDefaultSubobject` can only be used in a constructor since it uses the current `FObjectInitializer`. If there is no such initializer because no constructor is currently executing you crash (due to a failing assertion).
