# Can't assign a value to a variable in constructor

**URL:** <https://forums.unrealengine.com/t/cant-assign-a-value-to-a-variable-in-constructor/387180>\
**Category:** Editor Scripting\
**Tags:** variables, Variable, value, constructors, unreal-engine, CPP, question\
**Created:** [March 7, 2017, 8:22am UTC](https://forums.unrealengine.com/t/cant-assign-a-value-to-a-variable-in-constructor/387180 "2017-03-07T08:22:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_9f4a67d6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_9f4a67d6](https://forums.unrealengine.com/u/anonymous_user_9f4a67d6)\
**Post date:** [March 7, 2017, 8:22am UTC](https://forums.unrealengine.com/t/cant-assign-a-value-to-a-variable-in-constructor/387180/1 "2017-03-07T08:22:09Z")

</div>

Hello, I wanted to know why I can’t assign my variable to value in constructor?

MainCharacter.h

```
UCLASS()
class GAME_API AMainCharacter : public ACharacter
{
	GENERATED_BODY()

private:

	UPROPERTY(EditAnywhere, Category = "Needs | Helth")
	float CurrentHelth;

	UPROPERTY(EditAnywhere, Category = "Needs | Helth")
	float InitialHelth;

public:

    virtual void Tick(float DeltaTime) override;

    AMainCharacter();

    UFUNCTION(BlueprintPure, Category = "Needs | Helth")
    float GetInitialHelth();

    UFUNCTION(BlueprintPure, Category = "Needs | Helth")
    float GetCurrentHelth();

    virtual void BeginPlay() override;
};

```

MainCharacter.cpp

```
AMainCharacter::AMainCharacter()
{
	PrimaryActorTick.bCanEverTick = true;

	InitialHelth = 100.f;
	CurrentHelth = InitialHelth;
}

```

And when debug I see that InitialHelth = 0 and CurrentHelth.  
What am I doing wrong?

 ![128928-screenshot_100.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/7/2/972cd6ae970f000132fcbb8870f0b7b9f610a3c9.jpeg)

---

<div class="post-metadata">

**Author:** ![project.gheist](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@project.gheist](https://forums.unrealengine.com/u/project.gheist)\
**Post date:** [March 7, 2017, 9:11am UTC](https://forums.unrealengine.com/t/cant-assign-a-value-to-a-variable-in-constructor/387180/2 "2017-03-07T09:11:13Z")

</div>

Hi Sh1ftO\_o ,

I’m guessing that you are running a `Development` build, and that optimization is happening, resulting in the variables to be assigned in a different order. My guess is that you have to press F10 and look at the `AMainCharacter` variable after the construction is done (or even add a breakpoint to `Event Begin Play`), or change your configuration to `Debug`.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_9f4a67d6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_9f4a67d6](https://forums.unrealengine.com/u/anonymous_user_9f4a67d6)\
**Post date:** [March 8, 2017, 4:55pm UTC](https://forums.unrealengine.com/t/cant-assign-a-value-to-a-variable-in-constructor/387180/3 "2017-03-08T16:55:25Z")

</div>

Oh, thank you. It works. I select DebugGame Editor. You are the best !
