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
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    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:

    <code>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();
            }
        }
    }</code>

    OnTriggerEnter2D part in garlic script that is getting null reference:

    <code>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);
            }
        }
    }</code>
    #14885
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    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<GameObject>();
        }
    #14886
    Jason Hahne
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

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

    #14887
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

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

        if (collision.CompareTag("Enemy"))
        {
            EnemyStats enemy = collision.GetComponent<EnemyStats>();
            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
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    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: