Answer the question
In order to leave comments, you need to log in
Why is the object not initialized?
There is a parent class that has a monsterObj field
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()
{
}
}
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;
}
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());
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question