# How to NewObject with a UClass parameter?

**URL:** <https://forums.unrealengine.com/t/how-to-newobject-with-a-uclass-parameter/826769>\
**Category:** C++\
**Tags:** unreal-engine, CPP\
**Created:** [March 27, 2023, 4:38am UTC](https://forums.unrealengine.com/t/how-to-newobject-with-a-uclass-parameter/826769 "2023-03-27T04:38:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![krosshj](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/krosshj/32/88718_2.png) [@krosshj](https://forums.unrealengine.com/u/krosshj)\
**Post date:** [March 27, 2023, 4:38am UTC](https://forums.unrealengine.com/t/how-to-newobject-with-a-uclass-parameter/826769/1 "2023-03-27T04:38:09Z")

</div>

I want to do something like this:

```auto
void switchState(UClass* stateClass) {
	const FString stateClassName = stateClass->GetName();
	if (stateMap.Contains(stateClassName)) {
		state = stateMap[stateClassName];
	} else {
		state = NewObject<???>(this); // <- ??? should be what?
		state->characterRef = this;
		stateMap.Add(stateClassName, state);
	}
}

```

---

<div class="post-metadata">

**Author:** ![Chatouille](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chatouille/32/1194622_2.png) [@Chatouille](https://forums.unrealengine.com/u/Chatouille)\
**Post date:** [March 27, 2023, 7:58am UTC](https://forums.unrealengine.com/t/how-to-newobject-with-a-uclass-parameter/826769/2 "2023-03-27T07:58:35Z")

</div>

There is a version of NewObject that takes the class as parameter.  
The template type only specifies how to cast its return value.

```cpp
state = NewObject<UObject>(this, stateClass);

```
