Есть родительский класс, в котором есть поле monsterObj
public abstract class MonsterBehaviorBase : MonoBehaviour
{
protected IMonster monsterObj;
protected Transform tr;
protected float speed;
// Start is called before the first frame update
public virtual void Start()
{
tr = GetComponent<Transform>();
}
// Update is called once per frame
public virtual void Update()
{
Move();
}
public virtual void Move()
{
}
public virtual void OnCollisionEnter(Collision collision)
{
if (collision.collider.CompareTag("Player"))
{
monsterObj.Attack();
}
else
{
ChangeState();
}
}
public virtual void ChangeState()
{
}
}
Дочерний класс в котором monsterObj инициализируется
public class AttackingMonsterBehaviour : MonsterBehaviorBase
{
private MovingSystem moveSys;
private Transform movingSysObj;
// Start is called before the first frame update
public override void Start()
{
speed = 4f;
monsterObj = new AttakingMonster(tr.gameObject, Player.instance, Random.Range(10, 20));
movingSysObj = tr.parent;
InitScriptMovingSys Sys = movingSysObj.GetComponent<InitScriptMovingSys>();
moveSys = Sys.moveSys;
}
Система наследования для класса AttakingMonster:
public interface IMonster
{
void SetAttackClass(IAttack _class);
void Attack();
}
public abstract class MonsterBase : IMonster
{
protected Animator anim;
protected float attackharm;
public GameObject monster;
protected Player player;
protected IAttack attackclass;
public float speed { get; set; }
public MonsterBase(GameObject _monster, Player _player, float _attackharm)
{
monster = _monster;
anim = monster.GetComponent<Animator>();
player = _player;
attackharm = _attackharm;
speed = 3;
}
public void SetAttackClass(IAttack _class)
{
attackclass = _class;
}
public void Attack()
{
player.health -= attackharm;
attackclass.Attack(anim);
}
}
public class AttakingMonster : MonsterBase
{
public AttakingMonster(GameObject _monster, Player _player, float _attackharm) : base(_monster, _player, _attackharm)
{
monster = _monster;
anim = monster.GetComponent<Animator>();
player = _player;
attackharm = _attackharm;
SetAttackClass(new FighterAttack());
}
}
В результате monsterObj остаётся всегда null. Когда отлаживала, программа просто останавливалась на строчке, где создаётся объект. Почему так? спасибо за помощь