Instantiate is not copying properties of dynamically added component

Hello all,

There are several questions and answers surrounding Instantiate but none seem similar to my issue. Or perhaps I just couldn’t find the needle in the haystack.

I have a prefab which I instantiate in code. I then manually add a component and set certain variables on this component (int, enums, strings, lists & dictionaries), also in code.

At some point later I then want to make a duplicate of this gameobject, using instantiate. Unfortunately while a new object is created the added component does not have any of it’s variables set. I checked the original object just before the instantiate call, which has all the variables set correctly, but the new object variables on the component are all uninitialised.

This does not sound like the intended behaviour of Instantiate, is this because the component is added dynamically? How do I duplicate this object?

Thanks for any insight.

Instantiate only “instantiates” prefabs from your asset folder. Anything added later makes that instatiation an object of its own. If you want to keep the variables serialize them and make every new instatiation load those serialized variables into the variable slots.