Forum begins after the advertisement:


[Part 4] Unable to damage enemy

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #14883
    Jason Hahne
    Participant

    For both garlic and knife, I’m unable to damage the enemy. For the garlic script, there is a null reference on if(collision.CompareTag("Enemy") && !markedEnemies.Contains(collision.gameObject)) When I debugged projectileWeaponBehavior script, the only things that are getting triggered are player and collector. I think it is not supposed to get the collector as a collision in OnTriggerEnter2D since both the projectile and collector have 2d collider with isTrigger on. OnTriggerEnter2D part of projectileWeaponBehavior script:

    protected virtual void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Enemy"))
        {
            EnemyStats enemy = collision.GetComponent<EnemyStats>();
            enemy.TakeDamage(currentDamage);
            ReducePierce();
        }
        else if (collision.CompareTag("Prop"))
        {
            if(collision.gameObject.TryGetComponent(out BreakableProps breakable))
            {
                breakable.TakeDamge(currentDamage);
                ReducePierce();
            }
        }
    }

    OnTriggerEnter2D part in garlic script that is getting null reference:

    protected override void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.CompareTag("Enemy") && !markedEnemies.Contains(collision.gameObject))
        {
            EnemyStats enemy = collision.GetComponent<EnemyStats>();
            enemy.TakeDamage(currentDamage);
    
            markedEnemies.Add(collision.gameObject);
        }
        else if (collision.CompareTag("Prop"))
        {
            if (collision.gameObject.TryGetComponent(out BreakableProps breakable) && !markedEnemies.Contains(collision.gameObject))
            {
                breakable.TakeDamge(currentDamage);
                markedEnemies.Add(collision.gameObject);
            }
        }
    }
    #14885
    Terence
    Keymaster

    Hi Jason, did you tag your enemy prefabs “Enemy”? Make sure that the tag has the first character capitalised, and has no spaces before or after (e.g. “enemy” or “Enemy ” won’t work).

    For the garlic script, I suspect markedEnemies is null. Make sure that in the Start() function you initialise the variable:

        protected override void Start()
        {
            base.Start();
            markedEnemies = new List();
        }
    #14886
    Jason Hahne
    Participant

    I solved the null reference issue on garlic script, but wasn’t able to solve projectile not detecting enemy.

    #14887
    Terence
    Keymaster

    It’s very likely the enemy isn’t getting damaged because this line is failing:

        if (collision.CompareTag("Enemy"))
        {
            EnemyStats enemy = collision.GetComponent();
            enemy.TakeDamage(currentDamage);
            ReducePierce();
        }

    So just double check if your enemy prefabs are tagged correctly. Make sure you set the Tag, not the Layer.

    #14888
    Jason Hahne
    Participant

    I solved the problem. I forgot to put rigidbody2D. Thank you so much for helping me. :D

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: