From 98402a90f7e9d1ba05a46bee833b119231073871 Mon Sep 17 00:00:00 2001 From: trotFunky Date: Tue, 17 Sep 2019 20:30:20 +0200 Subject: [PATCH] Protect against NPE if no AbstractCharacter Made Speed a property to allow other Objects that are not Characters to use the Mover Init in Awake() to allow other objects to modify its properties during their Start() --- Assets/Scripts/Mover.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Assets/Scripts/Mover.cs b/Assets/Scripts/Mover.cs index e403f02..f044b7b 100644 --- a/Assets/Scripts/Mover.cs +++ b/Assets/Scripts/Mover.cs @@ -8,6 +8,11 @@ public class Mover : MonoBehaviour private Transform _transform; private Vector2 _direction; private float _maxSpeed; + public float MaxSpeed + { + get { return _maxSpeed; } + set { _maxSpeed = value; } + } public Vector2 Direction { @@ -15,11 +20,11 @@ public class Mover : MonoBehaviour set { _direction = value; } } - void Start() + void Awake() { _transform = GetComponentInParent(); _character = GetComponentInParent(); - _maxSpeed = _character.MaxSpeed; + _maxSpeed = _character != null ? _character.MaxSpeed : 1; _direction = Vector2.zero; }