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 /
avatar image
0
Question by Dalsia · Nov 22, 2018 at 03:12 AM · inspectorvariablesserializableserializefield

Why aren't my editor values saving when I press play?

I've read a lot of things online about how to fix this, but so far nothing has helped.

I'm trying to create a class that I do not want to inherit from MonoBehaviour since it should not be placed on a game object, but I still want to see and edit its values in the inspector. Here's a basic example:

 using UnityEngine;
 using System;
 
 [Serializable]
 public class Stats
 {
     public int health = 3;
     public float speed = 5;
 }

I've then created an instance of this Stats class in my "Character" class that is attached to the player's game object in Unity, serializing it so that it's shown in the inspector:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Character : MonoBehaviour
 {
     [SerializeField]
     private Stats stats;
 
     private void Start()
     {
         stats = new Stats();
     }
 }

This configuration successfully shows my Stats values in the inspector as part of the Character script. However, if I try to change a value in the inspector it is not saved on play, but rather reverts back to its default value ("3" for health, and "5" for speed). If I remove these default values in the Stats script then the values become "0" when pressing play.

Why is this happening, and how can I fix it? I am using a fresh install of Unity, but earlier this year I was working on another project with the same setup and did not have this issue at all, even though many of my scripts were configured this way.

If it helps, creating a variable in a Class that directly inherits from MonoBehaviour and marking it as serializable does not produce this problem. For example, this variable will be updated on play when changed in the inspector:

 public class TestClass: MonoBehaviour
 {
     [SerializeField]
     private string myName = "John";
 }
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 Dalsia · Nov 22, 2018 at 03:33 AM 0
Share

After looking at this some more, it appears that the line

 stats = new Stats();

may be the culprit. I'm not sure why this happens, though, so I would still appreciate if someone could help explain this to me.

avatar image Bonfire-Boy Dalsia · Nov 22, 2018 at 02:30 PM 0
Share

You're creating a new Stats object in that line, using the default constructor. What would you expect its values to be, if not those that are set by the default constructor?

Changing the values in the Inspector before hitting play does work, it's just that you are explicitly throwing those values away!

3 Replies

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

Answer by MisteryJ · Nov 22, 2018 at 12:56 PM

Unity deserializes the field before Start().Then in Start(), you set the stats to a new Stats, so the value is reset.

Start() is not constructor. If you put the code "stats = new Stats()" into constructor, or in the declaration "private Stats stats = new Stats();", the problem is not going to occur. After all, it is unnecessary to new a Stats. Unity's deserialization does this for you.

Comment
Add comment · Show 1 · 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 Dalsia · Nov 22, 2018 at 07:10 PM 0
Share

Thank you for clarifying; this indeed solved the problem.

avatar image
1

Answer by Nazirzadeh · Nov 22, 2018 at 05:01 AM

Try this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Character : MonoBehaviour
 {
     [SerializeField]
     private Stats stats;
     [SerializeField]
     private int Health;
     [SerializeField]
     private int Speed;
 
     private void Start()
     {
         stats = new Stats(Health,Speed);
     }
 }

and

 using UnityEngine;
 using System;
 
 [Serializable]
 public class Stats
 {
     private int _health;
     private float _speed;
     public Stats(int health,int speed)
     {
         _health = health;
         _speed = speed;
     }
 }
 

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
avatar image
0

Answer by austinsun102 · Nov 22, 2018 at 04:53 AM

You can try to inheritance the class to ScriptableObject and create a ScriptableObject. Try looking into ScriptableObjects. I hope that helped.

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

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

Custom abstract class (and inheritance) not allowing public array despite having System.Serializable? 1 Answer

Custom Inspector for an array of a serialized class 0 Answers

Starting Play mode resets values for serialized class in the Inspector 0 Answers

Using [SerializeField] vs public 2 Answers

Custom Display for system.object Editor 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