Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by hydrosis · Nov 21, 2016 at 09:54 AM · editorinspectorserializable

Why does Unity use the default values for my Serializable objects while running?

I have a few classes that have the Serializable attribute and I'm able to edit them just fine. When I hit play, though, it uses the default values. Once I stop the game, the values change back to the ones I set them to. I've read in some places that people have been extending the Editor class, but it doesn't look like the Unity guys are doing that in this video: https://youtu.be/M4bH9lWOJE4?t=18m13s

I've spent a few hours looking for a solution (e.g. EditorUtility.SetDirty), but that seems much more complicated than what how the Unity guys made TANKS!.

Below is the code I have. To keep it relevant, I just removed the functions that I have. If you think the functions I have are relevant, let me know and I'll update the question :)

GameManager.cs:

 public class GameManager : MonoBehaviour
 {
     public static GameManager Instance;
 
     public GameObject Camera;
     public GameObject MessageCanvas;
     public PlayerManager Player1;
 void Start () {
     if (Instance == null)
     {
         Instance = this;
     }
     else if (Instance != this)
     {
         Destroy(gameObject);
     }

     Player1 = new PlayerManager();
     Player1.Setup();

 }
 }

PlayerManager.CS:

 [Serializable]
 public class PlayerManager
 {
     public GameBoard PlayerBoard; 
     public void Setup()
     {
         PlayerBoard = new GameBoard();
         PlayerBoard.Initialize();
     }
 }

GameBoard.CS

 [Serializable]
 public class GameBoard
 {
     private Transform boardTransform;
 
     public Grid Player;
     public Grid Opponent;
     public GameBoard()
    {
         Player = new Grid();
         Opponent = new Grid();
    }

     public void Initialize()
     {
         Player.Initialize("Player Grid", boardTransform);
         Player.Initialize("Opponent Grid", boardTransform);
     }
 }

Grid.CS

 [Serializable]
 public class Grid
 {
     public int Width = 10;
     public int Height = 10;
     public float TileSize = 1;
     public Material TileMaterial;
     public Color TileColor = Color.yellow;
     [HideInInspector] public GameObject[][] Tiles;
     [HideInInspector] public GameObject instance;
     public Grid()
     {
         // Instansiate Tiles
     }
     public void Initialize(string name, Transform boardLocation)
     {
          // Initialize board here.
     }
 }

Before/After I hit play: alt text

While the game is running: alt text

screenshot-1.png (418.8 kB)
screenshot-2.png (405.2 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Adam-Mechtley · Nov 21, 2016 at 10:42 AM

Hi @hydrosis!

  1. Does your component have Start (), Awake (), OnEnable (), or ISerializationCallbackReceiver.OnAfterDeserialize ()? They may be relevant.

  2. Alternatively, is anything accessing GameManager.Instance when the game begins and doing anything with these properties?

  3. Does GameManager have a custom editor?

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 hydrosis · Nov 21, 2016 at 04:57 PM 0
Share

Thanks for the reply! I've modified my question to include the relevant functions.

  1. Only my Game$$anonymous$$anager implements $$anonymous$$onoBehaviour, so that one has a Start() function.

  2. $$anonymous$$y code currently isn't utilizing Instance, yet.

  3. Game$$anonymous$$anager doesn't have a custom editor.

avatar image Adam-Mechtley hydrosis · Nov 21, 2016 at 08:20 PM 0
Share

So the problem is that in your Start () method, you assign a new instance to the Player1 field, which completely erases the values that are deserialized for this field when the component is loaded. (Likewise, Player$$anonymous$$anager.Setup() reassigns the value for GameBoard, which assigns new Grids in its constructor.)

Basically, when Game$$anonymous$$anager.Start () is called, Player1 is already assigned and has read the serialized data for the instance, so there is no need to reassign it, to set up its fields or their fields, etc.

You might find this page in the docs helpful.

avatar image hydrosis Adam-Mechtley · Nov 22, 2016 at 02:41 AM 0
Share

Thanks a bunch! It all makes sense now :)

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

90 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 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 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 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 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

How can I detect when the name of a gameobject has been changed in the editor and do something when that happens? 2 Answers

How to create a drop down menu in editor inspector 0 Answers

Expose protected variable in inspector 1 Answer

,Unity Custom Inspector CreateInspectorGUI redraw on change 1 Answer

The variables modified in the inspector are not kept on build 0 Answers


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