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
1
Question by WickedSicked · Oct 09, 2017 at 07:25 PM · editor-scriptingserializationcustom-inspector

Persistently changing variables from custom editor

Hi everyone,

I have a script that has a custom class as a field, such as this:

 public class MyClass : Monobehaviour{
      public GameObject prefab; // a prefab that has a MyClass component
      public SomeClass someClass;
 }
 
 public class SomeClass{
      public int x;
 
      public SomeClass(int _x){
           x = _x;
      }
 }

What I'm trying to do is instantiate gameobjects with a MyClass component attached to them via a custom inspector for MyClass. Specifically, I want to set the someClass field to a specific value. Im currently trying it as follows:

 [CustomEditor (typeof(MyClass))]
 public class MyClassEditor : Editor{
      public override void OnInspectorGUI() {
         DrawDefaultInspector();
         
         MyClass mc = (MyClass) target;
 
         if(GUILayout.Button("button")) {
             GameObject go = (GameObject) Instantiate(mc.prefabToInstantiate);
             go.GetComponent<MyClass>().someClass.x = 1;
         }
      }
 }

This does indeed instantiate the desired object and sets x to 1. However, as soon as I start the game, the field x is not set to 1 anymore. Is there any way to make this value persistent, even when entering play mode?

Cheers.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by anilhdas · Oct 10, 2017 at 03:30 PM

You will need to make the SomeClass serializable for it to retain the data.

So what happens here is, when unity enters playmode, all the data that is stored in C# side (like the values in the inspector) is serialized and restored back after the assemblies are reloaded.

UPDATE: In case if you're wondering why did I specify "C# side", Unity's core engine code is split into 2 parts, managed (C#) and native (C++). In order to change the state of the editor (like Enter / Exit playmode for example), Unity undergoes an internal assembly reload.

So any data that needs to survive this assembly reload should be serializable by the editor.

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 WickedSicked · Oct 10, 2017 at 11:39 AM

I got it to work by making SomeClass inherit from ScriptableObject. I'm not sure exactly why this solves the problem, but it does. Maybe this will help someone in the future.

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

82 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

Related Questions

Custom editor, serialize and prefab issue 0 Answers

Custom Editor for Monobehavior with custom property not retaining Gameobject references 0 Answers

Custom inspector variable resets automatically 1 Answer

Custom Inspector - Method Selector just like Button OnClick()? 1 Answer

Is it possible to create a custom gettter/setter on SerializedProperty ? 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