Instantiate resets DateTime

The title says it all, but why does my DateTime variable (dato) reset when I instantiate?

            Debug.Log(card.GetComponent<CardScript>().dato.ToString("yyyy-MM-dd"));
            card = (GameObject) Instantiate(card,new Vector3(-7+(2*i),-3.4f,0), Quaternion.identity);
            Debug.Log(card.GetComponent<CardScript>().dato.ToString("yyyy-MM-dd"));

returns something like:
1900-05-06
0001-01-01

“Instantiate” means “to make a copy of”, so you’re creating a copy of an existing card object and then assigning it back to card, so the “card” that you access in the second debug is a brand new card, but with all of the components and public fields of those components copied over. That’s just some general background.

One important distinction: instantiating a prefab and instantiating an existing object in the scene are two different things. A prefab will copy all components and all data, public and private, whereas a GameObject will only try to duplicate it as best it can. Usually that means “just the public stuff”. I have no idea where the value in “dato” is drawn from, but if it’s a public property for a private backing field, that may explain why the value isn’t copied.

I have not delved deeply into this and don’t know how much of the process I grasp properly, but this may have to do with serialization. Try setting the [SerializeField] attribute on the backing field and see if that makes a difference when copying the GameObject. You also have the option of making any data that you want copied public, or creating a data transfer function where you pass the newly created object in as a parameter and copy the old data onto it.

1 Like

@DonLoquacious has it right. Only serialised data is copied across. This means most public fields, and any private fields marked with SerialiseField.

1 Like

That’s good, I was mostly making an educated guess >_>;

1 Like

I know. I have no longer use for the old card after this, only the instantiated object,so I’m just reusing the variable.

Not sure what this means, but it’s only a normal variable

public DateTime dato;

Make sure the class is set to be serializable with [System.Serializable] above the class definition (on the script that contains the “dato” variable). If that doesn’t work, try putting the [SerializeField] attribute over the “dato” definition. If that doesn’t work, post back here.

Nothing happened when I put it on the class. And I cant use it on a variable because its not a class, struct, enum or delegate. (According to Unity)

System.Serializable and SerializeField are two different attributes- the latter is the one I wanted you to try on the field.

Sorry, my bad. But it didn’t do anything either.

Is there something weird with DateTime? Because I used to have an int variable, and that worked fine, but when I swapped it with DateTime this happened.

I know I can just copy it from the old card to the new myself, but I just want to know why this is happening,

Yeah, it’s apparently a struct (which I did not know until I checked a minute ago), and structs can’t be serialized (aside from the Unity-specific ones like Vector3 and such). You’ll have to find a different way of getting the value across, or make a serialized class object that can carry all of the information you want from the DateTime and store that instead.

Do you actually need a lot of data from the DateTime object? If you just need a counter or something, you can store the value as a float instead. shrugs

Ok, thanks. :slight_smile:

You could write your own wrapper for DateTime which serializes it properly.

1 Like