# UPROPERTY member vars reset to NULL by ObjectInitializer

**URL:** <https://forums.unrealengine.com/t/uproperty-member-vars-reset-to-null-by-objectinitializer/19959>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [January 30, 2015, 3:41pm UTC](https://forums.unrealengine.com/t/uproperty-member-vars-reset-to-null-by-objectinitializer/19959 "2015-01-30T15:41:13Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![Alchemist\_1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alchemist_1/32/263274_2.png) [@Alchemist\_1](https://forums.unrealengine.com/u/Alchemist_1)\
**Post date:** [April 26, 2023, 6:01pm UTC](https://forums.unrealengine.com/t/uproperty-member-vars-reset-to-null-by-objectinitializer/19959/21 "2023-04-26T18:01:29Z")

</div>

In fact, the solution has been around for a long time, and the engine developers are using it.  
How this is implemented can be viewed in USplineComponent:

If we are talking about components, then in order to prevent resetting property values to predefined ones, we need to do 3 steps:

1. We need to define the FMyComponentInstanceData structure, which will store the values of properties that need to be restored when the component is recreated:

```auto
USTRUCT()
struct FMyComponentInstanceData : public FSceneComponentInstanceData {
    GENERATED_BODY()

public:
    // As an example, let's take a reference to instance of another component.
    // We will store 2 property values: current property value, and property value before user
    // construction script:

    /** Current property value. */
    UPROPERTY()
    TObjectPtr<UOtherComponent> ReferenceToOtherComponent;

    /** Before user construction script property value. */
    UPROPERTY()
    TObjectPtr<UOtherComponent> ReferenceToOtherComponentPreUCS;

public:
    FMyComponentInstanceData() {}
    explicit FMyComponentInstanceData(const UMyComponent* SourceComponent)
        : FSceneComponentInstanceData(SourceComponent) {}
    virtual ~FMyComponentInstanceData() = default;

    virtual void ApplyToComponent(UActorComponent* Component, const ECacheApplyPhase CacheApplyPhase) override {
        Super::ApplyToComponent(Component, CacheApplyPhase);
        CastChecked<UMyComponent>(Component)->ApplyComponentInstanceData(
                this, (CacheApplyPhase == ECacheApplyPhase::PostUserConstructionScript));
    }
};

```

1. Define the method in our component that will create and fill this structure:

```auto
virtual TStructOnScope<FActorComponentInstanceData> GetComponentInstanceData() const override
{
	TStructOnScope<FActorComponentInstanceData> InstanceData = MakeStructOnScope<FActorComponentInstanceData, FMyComponentInstanceData>(this);
	FMyComponentInstanceData* MyComponentInstanceData = InstanceData.Cast<FMyComponentInstanceData>();
	
    //	Store the property value
    MyComponentInstanceData->ReferenceToOtherComponent = ReferenceToOtherComponent;

	return InstanceData;
}

```

1. Define the method in our component that will restore the properties when component is recreated:

```auto
void ApplyComponentInstanceData(struct FMyComponentInstanceData* ComponentInstanceData, const bool bPostUCS)
{
    if (bPostUCS)
    {
        // Here we can handle changes in user construction script if needed.
        // If the property was changed in the user construction script, then there 
        // is no need to restore the property value:
        if(!IsReferenceToOtherComponentChangedInUCS())
        {
            // Restoring property value
            ComponentInstanceData->ReferenceToOtherComponent= ComponentInstanceData->ReferenceToOtherComponentPreUCS;
        }
    }
    else
    {
        ComponentInstanceData->ReferenceToOtherComponentPreUCS = ReferenceToOtherComponent;
    }
    
    ReferenceToOtherComponent = ComponentInstanceData->ReferenceToOtherComponent;
    
    SomeComponentUpdatesRelatedToPropertyChange();
}

```

Note:

- In this example, it is assumed that UMyComponent is a descendant from the USceneComponent class. Otherwise, creating and filling of the FMyComponentInstanceData structure may be somewhat more difficult to take into account the algorithm for restoring the properties of the parent component class.

- The UMyComponent class has the `TObjectPtr<UOtherComponent> ReferenceToOtherComponent` property, which refers to an instance of another component.

- The UMyComponent::IsReferenceToOtherComponentChangedInUCS() method code is not given because it depends on the meaning of the property being restored and the logic of the component (let me remind you once again that you can spy on the example of the implementation in the USplineComponent class in the source code of the engine).

---

<div class="post-metadata">

**Author:** ![AlienRenders](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AlienRenders](https://forums.unrealengine.com/u/AlienRenders)\
**Post date:** [May 7, 2023, 11:33pm UTC](https://forums.unrealengine.com/t/uproperty-member-vars-reset-to-null-by-objectinitializer/19959/22 "2023-05-07T23:33:50Z")

</div>

I’ve been pulling my hair out with this on 5.1. Well, that’s not all true. I’m bald. But still.

Here’s one solution that worked for me.

I renamed the property (create a define or something to avoid modifying too much code) and make sure you use a name you will never use. Then compile and open all the instances that used the component. Maybe you can just open the asset itself, but I just wanted to be sure. Change anything in the component and change it back so that you can resave it. I’m thinking it will now remove the old property from being serialized.

Close everything down and rename it back. Rebuild and relaunch.

It now works for me.

I think the temporary name is now serialized which is why you should have used something that you will never used. You can resave just to be sure.

edit: Never mind. It worked for a while and then it reverted to being null at some point.  
edit2: Ah, it’s that there seems to be different serialization for debug and release.

---

<div class="post-metadata">

**Author:** ![Guy\_Paddock](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Guy\_Paddock](https://forums.unrealengine.com/u/Guy_Paddock)\
**Post date:** [May 22, 2023, 3:10am UTC](https://forums.unrealengine.com/t/uproperty-member-vars-reset-to-null-by-objectinitializer/19959/23 "2023-05-22T03:10:33Z")

</div>

In my case, I was using the wrong way of constructing a UObject in the constructor. I was using `NewObject` but I needed to be using `CreateDefaultSubobject`. [This post on SO](https://stackoverflow.com/a/54776338/4342230) set me straight.

---

<div class="post-metadata">

**Author:** ![Online\_Learner\_6KMDI](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Online\_Learner\_6KMDI](https://forums.unrealengine.com/u/Online_Learner_6KMDI)\
**Post date:** [May 23, 2023, 5:05pm UTC](https://forums.unrealengine.com/t/uproperty-member-vars-reset-to-null-by-objectinitializer/19959/24 "2023-05-23T17:05:21Z")

</div>

It works for me.  
Epic, work too!

---

<div class="post-metadata">

**Author:** ![AlexanderSemenko](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexandersemenko/32/1068811_2.png) [@AlexanderSemenko](https://forums.unrealengine.com/u/AlexanderSemenko)\
**Post date:** [February 23, 2024, 2:54am UTC](https://forums.unrealengine.com/t/uproperty-member-vars-reset-to-null-by-objectinitializer/19959/25 "2024-02-23T02:54:38Z")

</div>

Confirming - there still this problem. The code which was working for a long time suddenly got broken and my object becomes creating with fields reinitialized by default object.

The fix proposed in this thread does solve my problem. In my case:

1. Just reparent your BP class to the basic UE (AActor in my case)

2. Compile / save. Your BP class may become broken - it’s OK.

3. Restart editor

4. Reparent you BP class to the correct basic class

5. Compile / save. Say Halleluya!

---

<div class="post-metadata">

**Author:** ![Wuffll](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wuffll](https://forums.unrealengine.com/u/Wuffll)\
**Post date:** [August 17, 2024, 1:28pm UTC](https://forums.unrealengine.com/t/uproperty-member-vars-reset-to-null-by-objectinitializer/19959/26 "2024-08-17T13:28:28Z")

</div>

Thank you. This worked for me.

[Previous page](https://forums.unrealengine.com/t/uproperty-member-vars-reset-to-null-by-objectinitializer/19959.md?page=1)
