# C++ project crashes when I try to add camera. Why?

**URL:** <https://forums.unrealengine.com/t/c-project-crashes-when-i-try-to-add-camera-why/1862316>\
**Category:** C++\
**Tags:** unreal-engine, crash, CPP, question\
**Created:** [May 18, 2024, 7:19pm UTC](https://forums.unrealengine.com/t/c-project-crashes-when-i-try-to-add-camera-why/1862316 "2024-05-18T19:19:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![wednescast\_arts](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/wednescast_arts/32/1047686_2.png) [@wednescast\_arts](https://forums.unrealengine.com/u/wednescast_arts)\
**Post date:** [May 18, 2024, 7:19pm UTC](https://forums.unrealengine.com/t/c-project-crashes-when-i-try-to-add-camera-why/1862316/1 "2024-05-18T19:19:51Z")

</div>

Good afternoon.

Recently, I’ve got a strange and annoying situation.  
Well, I wanted to create a C++ project from zero. Then, I was making a character class. And tried to add a camera.

FPCharacter.h

```auto
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Default")
class UCameraComponent* FPCamera;

```

FPCharacter.cpp:

```auto
FPCamera->CreateDefaultSubobject<UCameraComponent>(TEXT("FP Camera"));
FPCamera->SetupAttachment(GetCapsuleComponent());
FPCamera->bUsePawnControlRotation = true;

```

Which situation do I have? Well, when that piece of code in C++ file is deleted, Live Coding works ok. But when I add that code, I’m getting a crash of the engine.  
The video below is demonstrating that.

So how to deal with it? And how to avoid that absurd moment?  
Looking forward to your answer.

P.S.: btw, although I can fix “[disappearing C++ classes in Unreal Editor](https://forums.unrealengine.com/t/c-classes-are-disappearing-in-editor-but-they-are-appearing-in-visual-studio/783711)” bug in my project easily, but why isn’t it fixed in UE 5.4?

---

<div class="post-metadata">

**Author:** ![Bojann](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bojann/32/906560_2.png) [@Bojann](https://forums.unrealengine.com/u/Bojann)\
**Post date:** [May 18, 2024, 7:39pm UTC](https://forums.unrealengine.com/t/c-project-crashes-when-i-try-to-add-camera-why/1862316/2 "2024-05-18T19:39:16Z")

</div>

> [@wednescast\_arts](#):
>
> ```auto
> FPCamera->CreateDefaultSubobject<UCameraComponent>(TEXT("FP Camera"));
> FPCamera->SetupAttachment(GetCapsuleComponent());
> FPCamera->bUsePawnControlRotation = true;
> 
> ```

Instead of `FPCamera->CreateDefaultSubobject<UCameraComponent>(TEXT("FP Camera"));` use `FPCamera = CreateDefaultSubobject<UCameraComponent>(FName("FP Camera"));`

`FText` snuck in by there instead of `FName` :). Also, don’t use → but = to initialize the object.

Full code:

```
FPCamera = CreateDefaultSubobject<UCameraComponent>(FName("FP Camera"));
if (FPCamera != nullptr)
{
	FPCamera->SetupAttachment(GetCapsuleComponent());		
	FPCamera->bUsePawnControlRotation = true;
}

```

I would suggest to use Spring arm, so you can do it like this

```auto
	SpringArm = CreateDefaultSubobject<USpringArmComponent>(FName("SpringArm"));
	if (SpringArm != nullptr)
	{
		SpringArm->SetupAttachment(GetCapsuleComponent());
	}

	FPCamera = CreateDefaultSubobject<UCameraComponent>(FName("FP Camera"));
	if (FPCamera != nullptr && SpringArm != nullptr)
	{
		FPCamera->SetupAttachment(SpringArm);		
		FPCamera->bUsePawnControlRotation = true;
	}

```

---

<div class="post-metadata">

**Author:** ![wednescast\_arts](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/wednescast_arts/32/1047686_2.png) [@wednescast\_arts](https://forums.unrealengine.com/u/wednescast_arts)\
**Post date:** [May 18, 2024, 9:23pm UTC](https://forums.unrealengine.com/t/c-project-crashes-when-i-try-to-add-camera-why/1862316/3 "2024-05-18T21:23:35Z")

</div>

Unfortunately, it also didn’t work ☹  
The same crash.

Anyways, thx for tips.

---

<div class="post-metadata">

**Author:** ![Bojann](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bojann/32/906560_2.png) [@Bojann](https://forums.unrealengine.com/u/Bojann)\
**Post date:** [May 18, 2024, 9:26pm UTC](https://forums.unrealengine.com/t/c-project-crashes-when-i-try-to-add-camera-why/1862316/5 "2024-05-18T21:26:14Z")

</div>

Live coding does not work with constructors properly. Exit Unreal Engine, build the code and then run the engine again. I tested this code in my project, and it worked. So it must work on yours. I didn’t just type it here 🙂

---

<div class="post-metadata">

**Author:** ![Bojann](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bojann/32/906560_2.png) [@Bojann](https://forums.unrealengine.com/u/Bojann)\
**Post date:** [May 18, 2024, 9:30pm UTC](https://forums.unrealengine.com/t/c-project-crashes-when-i-try-to-add-camera-why/1862316/6 "2024-05-18T21:30:43Z")

</div>

What is the error you got now? Isn’t it different?

---

<div class="post-metadata">

**Author:** ![wednescast\_arts](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/wednescast_arts/32/1047686_2.png) [@wednescast\_arts](https://forums.unrealengine.com/u/wednescast_arts)\
**Post date:** [May 18, 2024, 9:39pm UTC](https://forums.unrealengine.com/t/c-project-crashes-when-i-try-to-add-camera-why/1862316/7 "2024-05-18T21:39:39Z")

</div>

I’m laughing right now because I used the code with Spring Arm (although I wanted First Person View) and it worked correctly, without any crashes! :)))))  
But if we look at FP template, we can see the camera without Spring Arm in the character class. Oh God, that’s really idiotic moment… 😕

---

<div class="post-metadata">

**Author:** ![Bojann](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bojann/32/906560_2.png) [@Bojann](https://forums.unrealengine.com/u/Bojann)\
**Post date:** [May 18, 2024, 9:45pm UTC](https://forums.unrealengine.com/t/c-project-crashes-when-i-try-to-add-camera-why/1862316/8 "2024-05-18T21:45:25Z")

</div>

Yeah, for FPS, there is no need for SpringArm. Not sure if I understand you corretly, but I assume you resolved the issue? If yes, then awesome 🙂

---

<div class="post-metadata">

**Author:** ![wednescast\_arts](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/wednescast_arts/32/1047686_2.png) [@wednescast\_arts](https://forums.unrealengine.com/u/wednescast_arts)\
**Post date:** [May 19, 2024, 9:46am UTC](https://forums.unrealengine.com/t/c-project-crashes-when-i-try-to-add-camera-why/1862316/9 "2024-05-19T09:46:11Z")

</div>

Solved but it was an eerie solution…  
It needs to be reported. I need to write to the UE developers because it is really absurd.  
The “disappearing C++ classes” bug isn’t still fixed. And SPRING ARM AS A SOLUTION KEY. Oh God, that’s really irritating moment. I don’t know why. I’m having a scream of my soul right now…

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [June 18, 2024, 9:46am UTC](https://forums.unrealengine.com/t/c-project-crashes-when-i-try-to-add-camera-why/1862316/10 "2024-06-18T09:46:48Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
