- Home /
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. :/
Your answer
Follow this Question
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