ShowInEditor attribute revert value to default
opened this issue · comments
Problem: When I change BallSpeed value to 100 in editor, press enter and click start simulation. Value back to 30
My code:
using System;
using System.Collections.Generic;
using FlaxEngine;
namespace Pong
{
public class ActorBall : Script
{
[ShowInEditor] private float BallSpeed = 30f;
private RigidBody rigidBody;
private void Awake()
{
rigidBody = this.Actor as RigidBody;
}
private void FixedUpdate()
{
rigidBody.LinearVelocity = Vector3.Right * BallSpeed;
}
}
}
That's because private
fields are not serialized by default.
To fix it just add Serialize
attribute to the BallSpeed field like that:
[ShowInEditor, Serialize] private float BallSpeed = 30f;
See serialization rules listed in a documentation here: https://docs.flaxengine.com/manual/scripting/serialization/index.html
Maybe attribute ShowInEditor should be serialized by default?
I'm not sure because it could be also used to debug private fields just to show them in properties panel without saving.
Attribute like this will be better for debug https://answers.unity.com/questions/489942/how-to-make-a-readonly-property-in-inspector.html