Skip to content

Instantly share code, notes, and snippets.

@Hotrian
Last active January 14, 2016 22:08
Show Gist options
  • Save Hotrian/3af96ad7c98221d8d8cc to your computer and use it in GitHub Desktop.
Save Hotrian/3af96ad7c98221d8d8cc to your computer and use it in GitHub Desktop.
using UnityEngine;
public class PlanePart : MonoBehaviour
{
[Header("Part Stats")]
public float Attack;
public float Defense;
public float Hp;
public float Weight;
#if UNITY_EDITOR
// This part allows the inspector to update outside of play mode
// See: http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnValidate.html
void OnValidate()
{
// Recalculate our stats when they change in the editor
if (!Application.isPlaying)
{
if (gameObject.transform != null && gameObject.transform.parent != null)
{
PlaneRoot parent = gameObject.transform.parent.GetComponent<PlaneRoot>();
if (parent != null)
{
parent.RecalcStats();
}
else
{
checkSelf();
}
}
else
{
checkSelf();
}
}
}
// Attempt to find our PlaneRoot and recalculate
void checkSelf()
{
PlaneRoot self = gameObject.GetComponent<PlaneRoot>();
if (self != null)
{
self.RecalcStats();
}
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment