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()
This commit is contained in:
trotFunky 2019-09-17 20:30:20 +02:00
parent ea9203ca8a
commit 98402a90f7

View file

@ -8,6 +8,11 @@ public class Mover : MonoBehaviour
private Transform _transform; private Transform _transform;
private Vector2 _direction; private Vector2 _direction;
private float _maxSpeed; private float _maxSpeed;
public float MaxSpeed
{
get { return _maxSpeed; }
set { _maxSpeed = value; }
}
public Vector2 Direction public Vector2 Direction
{ {
@ -15,11 +20,11 @@ public class Mover : MonoBehaviour
set { _direction = value; } set { _direction = value; }
} }
void Start() void Awake()
{ {
_transform = GetComponentInParent<Transform>(); _transform = GetComponentInParent<Transform>();
_character = GetComponentInParent<AbstractCharacter>(); _character = GetComponentInParent<AbstractCharacter>();
_maxSpeed = _character.MaxSpeed; _maxSpeed = _character != null ? _character.MaxSpeed : 1;
_direction = Vector2.zero; _direction = Vector2.zero;
} }