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
1
Question by ExciteMike · Apr 17, 2012 at 12:07 AM · customeditor

Changes made with custom editor not sticking.

One of my behaviors has a struct in it that I would like to be able to edit, so I made a custom editor for it. This lets me make edit it and make changes, but as soon as I hit the play button to test, my changes vanish. Any idea why the changes don't stick?

Here's a simple test case:

(editor script)

 using UnityEngine;
 using UnityEditor;
 [CustomEditor(typeof(TestComponent))]
 class TestEditorScript : Editor
 {
     public override void OnInspectorGUI()
     {
         TestComponent tc = target as TestComponent;
         TestComponent.TestStruct ts = tc.TestStructInst;
         
         ts.TestValue = EditorGUILayout.IntField(ts.TestValue);
         
         if (GUI.changed)
         {
             tc.TestStructInst = ts;
             EditorUtility.SetDirty(target);
             Debug.Log("Editor set value to " + tc.TestStructInst.TestValue.ToString());
         }
     }
 }


(this one attached to a game object)

 using System;
 using UnityEngine;
 public class TestComponent : MonoBehaviour
 {
     [Serializable]
     public struct TestStruct
     {
         public int TestValue;
     }    
     public TestStruct TestStructInst;
 
     void Start()
     {
         Debug.Log("But when playing the value is: " + TestStructInst.TestValue.ToString());
     }
 }

When changing the value in the editor, that Debug.Log in the editor script tells me I set the value to 100 or whatever like I typed. Once I hit play, the one in the component always says the value is zero, though.

Any idea what I'm missing?

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
Best Answer

Answer by ExciteMike · Apr 17, 2012 at 12:26 AM

It seems Unity simply doesn't know how to load up the settings for a struct. If I change it to a class instead, it works as expected.

It is not important that it be a struct instead of a class in this case, so I'll just do that for now.

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
1

Answer by Matsegui · Mar 05, 2013 at 01:01 PM

Hello, if you still have your problem, just replace EditorUtility.SetDirty(target) with EditorUtility.SetDirty(tc)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Draw inspector of array elements? 1 Answer

Editor performance: "CustomEditorAttributes" in Inspector 1 Answer

Why is ScriptableObject asset not saving to disk? 2 Answers

How to get fields of a custom script through SerializedProperty.FindPropertyRelative? 1 Answer

animation window custom scripting? Paste curve from one object onto another. 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