Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Lairinus · Oct 29, 2013 at 02:08 PM · c#objectserializationclassinstance

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!

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vexe · Oct 29, 2013 at 02:14 PM 0
Share

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.

avatar image btb200 · Oct 29, 2013 at 02:15 PM 0
Share

can you post some more code? where are you creating new baseClass ?

avatar image btb200 · Oct 29, 2013 at 02:23 PM 0
Share

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 ;)

avatar image Lairinus · Oct 29, 2013 at 02:26 PM 0
Share
 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.

avatar image vexe · Oct 29, 2013 at 02:33 PM 0
Share

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"

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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
 
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lairinus · Oct 29, 2013 at 03:26 PM 0
Share

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.

avatar image Yokimato · Oct 29, 2013 at 03:29 PM 0
Share

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.

avatar image Lairinus · Oct 29, 2013 at 03:37 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Trying to change a subclasses' variable 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges