Forum begins after the advertisement:
[Part 4] Unable to damage enemy
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 4] Unable to damage enemy
- This topic has 4 replies, 2 voices, and was last updated 6 months, 3 weeks ago by Jason Hahne.
-
AuthorPosts
-
May 31, 2024 at 11:04 pm #14883::
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>
June 1, 2024 at 12:28 am #14885::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 theStart()
function you initialise the variable:protected override void Start() { base.Start(); markedEnemies = new List<GameObject>(); }
June 1, 2024 at 12:29 am #14886::I solved the null reference issue on garlic script, but wasn’t able to solve projectile not detecting enemy.
June 1, 2024 at 12:32 am #14887::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.
June 1, 2024 at 12:51 am #14888::I solved the problem. I forgot to put rigidbody2D. Thank you so much for helping me. :D
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: