- Home /
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!
In essence, I now have two "startingRef" instances/objects.
That is only true if baseClass
was a value-type (like struct) which I don't think it is in your case. From the sound of it, baseClass
is a class so it's a reference type. $$anonymous$$eaning:
// create a startingRef reference in the stack
// to reference a newly created baseClass object in the heap
baseClass startingRef = new baseClass();
// creating a new baseClass reference in the stack
baseClass newRef;
// making it refer to the same object, that startingRef is referring to
newRef = startingRef;
In other words, startingRef
and newRef
are separate references referring to the same object - There is only one object/instance.
So if you modify what either of them is referring to, you're essentially modifying the same object.
can you post some more code? where are you creating new baseClass ?
does this script run in editor mode? if it's standard $$anonymous$$onoBeheaviour Unity will assign new Objects to each public variable and deserialize data that has in Inspector ;)
public class Test : $$anonymous$$onoBehaviour
{
public BaseClass startingRef = new BaseClass();
public BaseClass newRef;
void Start ()
{
newRef = startingRef;
newRef.Initialize();
}
}
This is the actual code, attached to a GameObject.
This is actually the first time I'm trying to make something like this work when it's not in a parameter. I'm not sure if your post was an answer, Vexe, but please explain if you can.
btb200:
It isn't marked as [ExecuteInEdit$$anonymous$$ode] because I did something else to make the serialized values pass on from class to class. The only real issue I'm having now is the "$$anonymous$$ake reference 1 and reference 2 equal the same class," thing.
I just wanted you to know that there's only one object/instance, and not two - like what you said -
Not sure what you meant with "I can ONLY modify the newRef"
Answer by Yokimato · Oct 29, 2013 at 03:18 PM
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
}
Thank you for posting the answer, but I think that I'm not being clear enough, or some information is getting lost.
I already know that two references point to the same object.
What I don't know is why I can't modify the Serialized values of both references in the inspector. I can only modify the newRef serialized fields in your first example. If I were to use your first example, I would NOT be able to modify the values of the original reference: startingRef.
To further clarify, if I set newRef = startingRef, and then right after set startingRef = newRef, I still can only modify the newRef values. It is behaving exactly like it should in every other aspect, just not in serialization.
btb200 stated that it might have something to do with the Editor GUI.
I understand; I would have to agree. The editor might not be capable of displaying both as editable since they're the same object. Did you have a use case for wanting to do so? or just trying stuff out?
if you had a use case for wanting to do so let us know and maybe we could help with a workaround. Either way, I'd submit a bug since you seem to have found one.
I'll go ahead with submitting a bug.
As stated, I'll probably go ahead with creating a "BaseClass_Serialized" that i'll directly link with my "BaseClass"
This started out as one of those things that I tried to see if I could do. Then I got infatuated with the idea of being able, and didn't want to do it any other way.
Thanks to everyone who helped with this... if anyone finds an answer though, definitely shed some light :D
Your answer
Follow this Question
Related Questions
C#: Serialize a class as Field 1 Answer
Get instance of exisiting object 1 Answer
Multiple Cars not working 1 Answer
C# NullRefException accessing static class, array value 1 Answer