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 Kali2048 · Feb 09, 2015 at 11:46 AM · c#instantiateserializationserializablecustom class

Instantiating a serializable class and variable

Here is what I'm doing (Shorter version :D)

  • I've created an empty serializable class with only a public integer variable "myNumber" inside it.

  • I then create an instance "A" of the serializable class. copy "A" to a new variable "B".

  • in the editor set the variable "myNumber" inside "A" and see what happen to "myNumber" in "B".

  • in the editor set the variable "myNumber" inside "B" and see what happen to "myNumber" in "A".

    Well, what happen is that when you change "B" then "A" changes as well, so they seem to point to the same variable.

    But when you change "A" nothing happens, not even "A" changes.

What's going on with the Serializable class ?

Why only B can be changed ?

Any explanation would be really welcome !

So here are the scripts :


 // caller script attached to a game object and used to instantiate my serializable classe
 using UnityEngine;
 
 public class TestClassCaller : MonoBehaviour {
     public TestClassSerializable serializableA, serializableB;
 
     void Start () {
         serializableA = new TestClassSerializable();
         serializableB = serializableA;
     }
 }

 // Serializable class
 [System.Serializable]
 public class TestClassSerializable {
     public int myNumber;
 }

Comment
Add comment · Show 2
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 fafase · Feb 11, 2015 at 05:03 AM 0
Share
 Class objectA = new Class();
 Class objectB = objectA;

This means A and B are pretty much the same thing, two references to the same address in memory. What you need is a deep copy.

 [System.Serializable]
  public class TestClassSerializable {
      public int myInt;
      public string myString;
      public  TestClassSerializable  DeepCopy(){
          TestClassSerializable temp = new TestClassSerializable ();
          temp.myInt = this.myInt;
          temp.myString = this.myString;
          return temp;
      }
  }

This will create a new object, totally independant from the the first but with the values of the first. Now when you change one, the other remains.

Note that if your object contains an object reference as member then you need to do the same process of deep copy or your two different objects will have a common reference.

I would think the ScriptableObject class has an override of the equal sign and this is why you do not see this problem.

avatar image Kali2048 · Feb 11, 2015 at 05:35 AM 0
Share

This means A and B are pretty much the same thing, two references to the same address in memory. What you need is a deep copy.

That's the problem right here, you'd expect that if I do A = B = C they would all point to the same thing or be passed by value ... but they are not, only B can be changed and values will be the same in A and $$anonymous$$ain. But if you try to change A or $$anonymous$$ain, no value is set, not even on A or $$anonymous$$ain. It behave like B is overwriting A and $$anonymous$$ain.

I just want to be able to change A or B or $$anonymous$$ain and be able to see the same changes in other instances.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Baalhug · Nov 30, 2018 at 07:59 PM

This is what documentation says about that:

The Inspector window

When you view or change the value of a GameObject ’s component field in the Inspector window, Unity serializes this data and then displays it in the Inspector window. The Inspector window does not communicate with the Unity Scripting API when it displays the values of a field.

If you use properties in your script, any of the property getters and setters are never called when you view or change values in the Inspector windows as Unity serializes the Inspector window fields directly. This means that: While the values of a field in the Inspector window represent script properties, changes to values in the Inspector window do not call any property getters and setters in your script

from here

But that does not explain the normal behaviour of changing properties in inspector while running the game and changes actually affect the game.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Error serializing a class to save / load 2 Answers

Instantiated objects into serialized property? 2 Answers

Distribute terrain in zones 3 Answers

How can I get functionality similar to OnValidate for a custom class? 4 Answers

Multiple Cars not working 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