# How to construct object from \`TSubclassOf\<UMyObject\>\`

**URL:** <https://forums.unrealengine.com/t/how-to-construct-object-from-tsubclassof-umyobject/674789>\
**Category:** C++\
**Tags:** unreal-engine, UE5-0, CPP\
**Created:** [October 22, 2022, 11:31am UTC](https://forums.unrealengine.com/t/how-to-construct-object-from-tsubclassof-umyobject/674789 "2022-10-22T11:31:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zos](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@zos](https://forums.unrealengine.com/u/zos)\
**Post date:** [October 22, 2022, 11:31am UTC](https://forums.unrealengine.com/t/how-to-construct-object-from-tsubclassof-umyobject/674789/1 "2022-10-22T11:31:57Z")

</div>

Suppose I have some `UObject` subclass called `UMyObject` and I have a `TSubclassOf<UMyObject>` `MyClass` that is a blueprint class derived from `UMyObject`, how do I create a new instance of `MyClass` and get a `UMyObject*` pointer to it?

```auto
UCLASS()
class UMyObject : public UObject { /*...*/ }

```

```auto
UMyObject* MakeMyObjectFromBlueprint(TSubclassOf<UMyObject> MyClass) {
    return ???;
}

```

---

<div class="post-metadata">

**Author:** ![zos](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@zos](https://forums.unrealengine.com/u/zos)\
**Post date:** [October 22, 2022, 5:17pm UTC](https://forums.unrealengine.com/t/how-to-construct-object-from-tsubclassof-umyobject/674789/2 "2022-10-22T17:17:58Z")

</div>

It’s just:

```
UMyObject* MyObject = NewObject<UMyObject>(GetTransientPackage(), MyClass);

```

---

<div class="post-metadata">

**Author:** ![Gulith421](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Gulith421](https://forums.unrealengine.com/u/Gulith421)\
**Post date:** [June 25, 2024, 3:08pm UTC](https://forums.unrealengine.com/t/how-to-construct-object-from-tsubclassof-umyobject/674789/3 "2024-06-25T15:08:43Z")

</div>

> [@zos](#):
>
> `GetTransientPackage(), `

doesn’t work properly for me …  
it has very weird behavior, like the object is only half valid … it looks fine until i use a function from it, and then it acts like a nullptr obj …  
the object works fine when i build it with ConstructObjectFromClass with blueprint.

---

<div class="post-metadata">

**Author:** ![zeaf](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/zeaf/32/1108035_2.png) [@zeaf](https://forums.unrealengine.com/u/zeaf)\
**Post date:** [June 25, 2024, 3:19pm UTC](https://forums.unrealengine.com/t/how-to-construct-object-from-tsubclassof-umyobject/674789/4 "2024-06-25T15:19:59Z")

</div>

If you don’t keep a strong reference to the object, it will get garbage collected eventually

Having a pointer member variable marked with UPROPERTY() will keep the object alive for as long as that variable exists

It depends on your case, but if the object that spawns the new object has a

```auto
UPROPERTY(EditAnywhere)
TSubclassOf<UMyObject> MyClass;

UPROPERTY()
TObjectPtr<UMyObject> MySpawnedObject;

```

and then you set MySpawnedObject when spawning, it should stay alive

```auto
MySpawnedObject = NewObject<UMyObject>(this, MyClass);

```

A bit more advanced, but you can also use `AddToRoot()` if you don’t store it to a member, and that will make the object unaffected by garbage collection, but then you need to keep track of it, and remove it from root respectively when not needed anymore

---

<div class="post-metadata">

**Author:** ![Gulith421](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Gulith421](https://forums.unrealengine.com/u/Gulith421)\
**Post date:** [June 25, 2024, 3:26pm UTC](https://forums.unrealengine.com/t/how-to-construct-object-from-tsubclassof-umyobject/674789/5 "2024-06-25T15:26:42Z")

</div>

oh thanks, that’s why!  
for now i’ll just use BP\_ContructObjectFromClass … i don’t need to make objects in mass =).
