# "Using incorrect object initializer" while initializing USpotLightComponent

**URL:** <https://forums.unrealengine.com/t/using-incorrect-object-initializer-while-initializing-uspotlightcomponent/687395>\
**Category:** C++\
**Tags:** unreal-engine, UE4, UE4-27, question, CPP\
**Created:** [November 9, 2022, 3:56pm UTC](https://forums.unrealengine.com/t/using-incorrect-object-initializer-while-initializing-uspotlightcomponent/687395 "2022-11-09T15:56:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![EgorMaksM](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/egormaksm/32/548054_2.png) [@EgorMaksM](https://forums.unrealengine.com/u/EgorMaksM)\
**Post date:** [November 9, 2022, 3:56pm UTC](https://forums.unrealengine.com/t/using-incorrect-object-initializer-while-initializing-uspotlightcomponent/687395/1 "2022-11-09T15:56:02Z")

</div>

So, I tried to add Flashlight to my First Person game:  
.h file:

```auto
UPROPERTY(EditAnywhere)
		class USpotLightComponent* FlashlightComponent;

```

.cpp file:

```auto
FlashlightComponent->CreateDefaultSubobject<USpotLightComponent>(TEXT("Light"));

```

But after attempt to compile project, UE crashed, and here is screenshot:

 ![image1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/f/d/6fd6518cfe3b47f2418513dbb64825abc8f99f1e.png)  
Any ideas?

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [November 9, 2022, 4:00pm UTC](https://forums.unrealengine.com/t/using-incorrect-object-initializer-while-initializing-uspotlightcomponent/687395/2 "2022-11-09T16:00:02Z")

</div>

Where in the cpp file are you initializing the variable?? In the CDO?

---

<div class="post-metadata">

**Author:** ![EgorMaksM](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/egormaksm/32/548054_2.png) [@EgorMaksM](https://forums.unrealengine.com/u/EgorMaksM)\
**Post date:** [November 9, 2022, 4:04pm UTC](https://forums.unrealengine.com/t/using-incorrect-object-initializer-while-initializing-uspotlightcomponent/687395/3 "2022-11-09T16:04:05Z")

</div>

Sorry, im very new into Unreal, what is CDO? If I understand right, and my charcter’s class name is AMyCharacterCPP, then CDO is AMyCharacterCPP::AMyCharacterCPP func. If so, yes, im initializing it in CDO.

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [November 9, 2022, 4:32pm UTC](https://forums.unrealengine.com/t/using-incorrect-object-initializer-while-initializing-uspotlightcomponent/687395/4 "2022-11-09T16:32:30Z")

</div>

> [@EgorMaksM](#):
>
> `FlashlightComponent->CreateDefaultSubobject<USpotLightComponent>(TEXT("Light"));`

Didn’t noticed you called CreateDefaultSubobject on FlashlightComponent…  
FlashlightComponent is the object that is supposed to be initialized. You can’t call CreateDefaultSubobject because it doesn’t exist yet.

You need to call:  
FlashlightComponent = CreateDefaultSubobject(TEXT(“Light”));

and you were putting it in the CDO (Class Default Object). It’s the part of code that in part resembles a pure c++ constructor where default values are set.  
It has the same method name as the class so in your case  
AMyCharacterCPP::AMyCharacterCPP though the passed in parameters can be different depending on implementation.

---

<div class="post-metadata">

**Author:** ![EgorMaksM](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/egormaksm/32/548054_2.png) [@EgorMaksM](https://forums.unrealengine.com/u/EgorMaksM)\
**Post date:** [November 9, 2022, 4:51pm UTC](https://forums.unrealengine.com/t/using-incorrect-object-initializer-while-initializing-uspotlightcomponent/687395/5 "2022-11-09T16:51:37Z")

</div>

> [@3dRaven](#):
>
> FlashlightComponent = CreateDefaultSubobject(TEXT(“Light”));

No, it didn’t work.

 ![зображення](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/e/7/ce79476fb2ebb768b61cabd157304d165ed16050.png)

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [November 9, 2022, 4:54pm UTC](https://forums.unrealengine.com/t/using-incorrect-object-initializer-while-initializing-uspotlightcomponent/687395/6 "2022-11-09T16:54:28Z")

</div>

Copy + paste error (you need to set the return type)

```
FlashlightComponent = CreateDefaultSubobject<USpotLightComponent>(TEXT(“Light”));

```

---

<div class="post-metadata">

**Author:** ![EgorMaksM](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/egormaksm/32/548054_2.png) [@EgorMaksM](https://forums.unrealengine.com/u/EgorMaksM)\
**Post date:** [November 9, 2022, 4:56pm UTC](https://forums.unrealengine.com/t/using-incorrect-object-initializer-while-initializing-uspotlightcomponent/687395/7 "2022-11-09T16:56:20Z")

</div>

|Error (active)|E0304|no instance of overloaded function AMyCharacterCPP::CreateDefaultSubobject matches the argument list

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [November 9, 2022, 5:02pm UTC](https://forums.unrealengine.com/t/using-incorrect-object-initializer-while-initializing-uspotlightcomponent/687395/8 "2022-11-09T17:02:17Z")

</div>

Double check your code…you literally have the same syntax 1 line above. Perhaps your quotation marks are wrong?

Also make sure your cpp has:  
#include “Components/SpotLightComponent.h”

---

<div class="post-metadata">

**Author:** ![EgorMaksM](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/egormaksm/32/548054_2.png) [@EgorMaksM](https://forums.unrealengine.com/u/EgorMaksM)\
**Post date:** [November 9, 2022, 5:11pm UTC](https://forums.unrealengine.com/t/using-incorrect-object-initializer-while-initializing-uspotlightcomponent/687395/9 "2022-11-09T17:11:10Z")

</div>

Yes, the code is ok. The only difference between this line and one above it, is that it doesn’t have (or however its called) parameter

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [November 9, 2022, 5:14pm UTC](https://forums.unrealengine.com/t/using-incorrect-object-initializer-while-initializing-uspotlightcomponent/687395/10 "2022-11-09T17:14:50Z")

</div>

It needs the uspotlightcomponent class passed in the angular brackets

---

<div class="post-metadata">

**Author:** ![EgorMaksM](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/egormaksm/32/548054_2.png) [@EgorMaksM](https://forums.unrealengine.com/u/EgorMaksM)\
**Post date:** [November 9, 2022, 5:21pm UTC](https://forums.unrealengine.com/t/using-incorrect-object-initializer-while-initializing-uspotlightcomponent/687395/11 "2022-11-09T17:21:39Z")

</div>

Wait, what? Yes, it worked, but HOW? When I asked this question, the code was just the same. Is it some bug?
