- Home /
Question by
MaksimumYT · Jan 24, 2020 at 08:30 AM ·
instantiate2d game2d-physics
Stacked Bullets on Instantiation
Hello. When I fire off a bullet in my game, the ammo count goes down by one, meaning there is one trigger. However, in the Debug.Log it shows that there were two simultaneous collisions. The bullets seem to be stacked, so my enemies are taking double damage.
Code for bullet prefab:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Bullet : MonoBehaviour {
// Start is called before the first frame update
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.name == "Crate")
{
Destroy(gameObject);
}
if (collision.gameObject.name == "Zombie")
{
Destroy(gameObject);
collision.GetComponent<EnemyHealth>().currentHealth = collision.GetComponent<EnemyHealth>().currentHealth - GameObject.Find("Player").GetComponent<PlayerEquipment>().Weapon.GetComponent<Gun>().damage;
Debug.Log(collision.GetComponent<EnemyHealth>().currentHealth);
}
if (collision.gameObject.name == "Midground")
{
Destroy(gameObject);
}
Debug.Log(collision);
}
}
Code for bullet Instantiation:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Bullet : MonoBehaviour {
// Start is called before the first frame update
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.name == "Crate")
{
Destroy(gameObject);
}
if (collision.gameObject.name == "Zombie")
{
Destroy(gameObject);
collision.GetComponent<EnemyHealth>().currentHealth = collision.GetComponent<EnemyHealth>().currentHealth - GameObject.Find("Player").GetComponent<PlayerEquipment>().Weapon.GetComponent<Gun>().damage;
Debug.Log(collision.GetComponent<EnemyHealth>().currentHealth);
}
if (collision.gameObject.name == "Midground")
{
Destroy(gameObject);
}
Debug.Log(collision);
}
}
Comment