Components/PhysicalProperties.cs

Component that stores physical properties (Mass, GravityScale, Health) and applies them to the root GameObject's Rigidbody and Prop components when started or enabled. It persists values via Property and Sync attributes and sets Rigidbody.MassOverride, Rigidbody.GravityScale, and Prop.Health if positive values are provided.

🐞 The code assumes GameObject.Root.GetComponent<T>() returns a non-null object and calls IsValid() on it; if GetComponent returns null this will throw a NullReferenceException before IsValid can be called.
/// <summary>
/// Persists physical properties (mass, gravity, health) across duplication and networking.
/// Attach this to any GameObject to ensure these values survive serialization.
/// </summary>
public sealed class PhysicalProperties : Component
{
	[Property, Sync]
	public float Mass { get; set; } = 0f;

	[Property, Sync]
	public float GravityScale { get; set; } = 1f;

	[Property, Sync]
	public float Health { get; set; } = 0f;

	protected override void OnStart() => Apply();
	protected override void OnEnabled() => Apply();

	public void Apply()
	{
		var rb = GameObject.Root.GetComponent<Rigidbody>();
		if ( rb.IsValid() )
		{
			if ( Mass > 0f ) rb.MassOverride = Mass;
			rb.GravityScale = GravityScale;
		}

		if ( Health > 0f )
		{
			var prop = GameObject.Root.GetComponent<Prop>();
			if ( prop.IsValid() ) prop.Health = Health;
		}
	}
}