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 ShroomWasTaken · Jan 07, 2018 at 02:07 PM · editor-scriptingserializedpropertyserializedobject

Calling ApplyModifiedProperties results in other variables resetting to 0?

I'm having a weird problem where I have two classes called Platfom and PlatformEditor. PlatformEditor has a SerializedProperty and SerializedObject of a variable called _endPoint in PlatformEditor. Once a frame I set the SerializedProperty for _endPoint to a transform.position, and then I call ApplyModifiedProperties() on the SerializedObject. For some reason, two other variables, both bools seem to reset to 0 (false) when I the function. But when I remove it the ApplyModifiedProperties line, the variables keep the value like they should.



I'm not at all sure which part of my code which is causing this problem, but here's what I think it could be:

PlatformEditor.cs:

 using UnityEngine;
 using UnityEditor;
 using System;
 
 [CustomEditor(typeof(Platform))]
 public class PlatformEditor : EditorBase<Platform>
 {
     private static GameObject _destinationObject;
 
     private SerializedObject _endPointObject;
     private SerializedProperty _endPointProperty;
 
     public override void OnEnable()
     {
         base.OnEnable();
 
         Debug.Log("Enabled");
 
         EditorApplication.update += OnEditorUpdate;
 
         _endPointObject = new SerializedObject(target);
         _endPointProperty = _endPointObject.FindProperty("endPoint");
     }
 
     private void OnEditorUpdate()
     {
         if (targetComponent.IsEditingDestination)
         {
             _endPointProperty = _endPointObject.FindProperty("endPoint");
             _endPointProperty.vector3Value = _destinationObject.transform.position;
             
             _endPointObject.ApplyModifiedProperties(); // This is the line which seemingly resets the variables in Platform.cs.
         }
         else if (targetComponent.IsPreviewingDestination)
         {
             throw new NotImplementedException();
         }
     }
 
     private void OnDisable()
     {
         EditorApplication.update -= OnEditorUpdate;
     }
 
 }



Platform.cs:

 using UnityEditor;
 using UnityEngine;
 
 public class Platform : BehaviourBase {
 
     public Vector3 endPoint;
 
     public bool IsPreviewingDestination;
     public bool IsEditingDestination;
 
     private void OnDrawGizmos()
     {
         if (IsPreviewingDestination) // Due to the fact that this bool seems to reset, this block doesn't run when it should.
         {
             Debug.Log("EndPoint on preview is: " + endPoint);
 
             Gizmos.color = Color.red;
             Gizmos.DrawLine(transform.position, endPoint);
 
             Gizmos.color = Color.green;
             Gizmos.DrawSphere(endPoint, 1.0f);
             Gizmos.DrawSphere(transform.position, 1.0f);
 
             Gizmos.color = Color.yellow;
             Gizmos.DrawWireMesh(GetComponent<MeshFilter>().sharedMesh, endPoint, Quaternion.identity, transform.localScale);
 
             Gizmos.color = Color.blue;
             Gizmos.DrawWireMesh(GetComponent<MeshFilter>().sharedMesh, endPoint, Quaternion.identity, transform.localScale);
 
             Gizmos.color = Color.white;
         }
     }
 }




Here are links to the full scripts if needed:
Platform.cs
PlatformEditor.cs
EditorHelpers.cs



Edit 1:
OK I'm confused, for some reason turning the IsEditingDestination variable into a property made it not reset any of the values, including the IsPreviewing variable (which still is a variable and not a property).
How is this possible? o.O

Edit 2:
Turns out making a property didn't fix it, must have been a coincidence that it worked when I added it and stopped working when I removed it. But now it doesn't matter if I have a property or a variable, doesn't work either way. :/

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

0 Replies

· Add your reply
  • Sort: 

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

81 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

Related Questions

Get UnityEvent reference from SerializedProperty 1 Answer

Replicate "Apply" and "Revert" button functionality in ScriptableObject Editor Inspector 2 Answers

Changing isExpanded property from another Editor 0 Answers

Call a function of a custom class through a SerializedProperty 0 Answers

Editor-Scripting : event for a serialized property changed 3 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