Serialization - Variables won't change on original construction

Hey guys,

This is what I’m doing:

        baseClass startingRef = new baseClass();
        baseClass newRef;

        newRef = startingRef;

In essence, I now have two “startingRef” instances/objects.

Why is it then that in the inspector, if both are serialized, I can ONLY modify the newRef? Every time I adjust any field on the startingRef, it gets reset to what it was. If I modify the newRef, however, it also changes the startingRef values. (What I want, except opposite)

Also, setting it back (startingRef = newRef) has no effect.

Thanks for any help!

Hopefully I can make some sense of this.

Vexe was correct in saying that you do not have 2 objects, you have 1. You have 2 references and 1 object. Both references point to the same object so…

public BaseClass startingRef = new BaseClass();

public BaseClass newRef;

void Start() {
  newRef = startingRef;
  // now newRef and startingRef BOTH point to the same object

  newRef.EditValue = 5;

  //now newRef.EditValue will be 5 AND startingRef.EditValue will be 5 since they point to the SAME object

}

If your goal is to have them not working in sync, you would need to assign a different BaseClass instance like so:

public BaseClass startingRef = new BaseClass();

public BaseClass newRef;

void Start() {
  newRef = new BaseClass()
  // now newRef and startingRef point to 2 different objects

  newRef.EditValue = 5;

  //now newRef.EditValue will be 5 BUT startingRef.EditValue will be NOT 5 since they point to different objects

}