P
P
PyChan2020-07-05 14:36:49
C++ / C#
PyChan, 2020-07-05 14:36:49

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()
    {

    }
}

Child class in which monsterObj is initialized
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;
    }

Inheritance system for the AttakingMonster class:
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());
    }
}

As a result, monsterObj is always null. When debugging, the program simply stopped at the line where the object was created. Why is that? thanks for the help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2020-07-05
@PyChan

Because you have at the moment

monsterObj = new AttakingMonster(tr.gameObject, Player.instance, Random.Range(10, 20));

tr null. In general, you always have it null, because you do not call the base methodStart

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question