Bullet hit a object, another object get destroyed
So this this my problem, i have a square that when destroyed spawn 4 smaller squares when shooting on it, but when i shoot one of the 4 smaller squares sometimes a diferent square that are not even close gets destroyed.
The problem is worse when it have multiple smaller squares because just shooting one of the squares continuously make the other ones get destroy except the one i'm shooting
Asteroid Code (Square)
public class AsteroidHealth : MonoBehaviour
{
[SerializeField] private int maxHealth = 1;
public int currentHealth;
public GameObject miniAsteroid;
// Start is called before the first frame update
void Awake()
{
currentHealth = maxHealth;
}
// Update is called once per frame
void Update()
{
DestructionAsteroid();
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Magnet")
{
currentHealth--;
}
}
//Asteroid
void DestructionAsteroid()
{
if (gameObject.tag == "Asteroid" && currentHealth < 1)
{
for (int i = 0; i < 4; i++)
{
float randomPos = Random.Range(-10, 10);
Instantiate(miniAsteroid, transform.position + new Vector3(randomPos, 0, randomPos), transform.rotation);
}
Destroy(gameObject);
}
}
}
Mini Asteroid (Smaller Square)
public class MiniAsteroidHealth : MonoBehaviour
{
[SerializeField] private int maxHealth = 1;
public int currentHealth;
private SpawnManager spawnManagerScript;
private bool collisionMagnet = false;
public GameObject ore;
private GameManager gameManagerScript;
// Start is called before the first frame update
void Awake()
{
currentHealth = maxHealth;
gameManagerScript = GameObject.Find("GameManager").GetComponent<GameManager>();
}
// Update is called once per frame
void Update()
{
DestroyMiniAsteroid();
}
void DestroyMiniAsteroid()
{
if (gameObject.tag == "MiniAsteroid" && currentHealth < 1 && collisionMagnet == false)
{
spawnManagerScript = GameObject.Find("Spawn Manager").GetComponent<SpawnManager>();
spawnManagerScript.miniAsteroidDestroy++;
float randomPos = Random.Range(-5, 5);
Instantiate(ore, transform.position + new Vector3(randomPos, 0, randomPos), transform.rotation);
Destroy(gameObject);
}
}
void Absorption()
{
if (gameObject.tag == "MiniAsteroid" && currentHealth < 1)
{
spawnManagerScript = GameObject.Find("Spawn Manager").GetComponent<SpawnManager>();
gameManagerScript.UpdateScore(5);
spawnManagerScript.miniAsteroidDestroy++;
Destroy(gameObject);
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Magnet")
{
currentHealth--;
collisionMagnet = true;
Absorption();
}
}
}
Bullet Code
public class BulletSettings : MonoBehaviour
{
[SerializeField] private float bulletSpeed = 100;
private AsteroidHealth asteroidHealthScript;
private MiniAsteroidHealth miniAsteroidHealthScript;
public float lifeSpan = 1.5f;
public int bulletDamage = 1;
public GameObject explosion;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
Movement();
DestroyAfterTime();
}
private void OnCollisionEnter(Collision collision)
{
Instantiate(explosion, transform.position, transform.rotation);
if (collision.gameObject.tag == "Asteroid")
{
asteroidHealthScript = GameObject.FindWithTag("Asteroid").GetComponent<AsteroidHealth>();
asteroidHealthScript.currentHealth -= bulletDamage;
}
if(collision.gameObject.tag == "MiniAsteroid")
{
miniAsteroidHealthScript= GameObject.FindWithTag("MiniAsteroid").GetComponent<MiniAsteroidHealth>();
miniAsteroidHealthScript.currentHealth -= bulletDamage;
}
Destroy(gameObject);
}
void Movement()
{
transform.Translate(Vector3.forward * bulletSpeed * Time.deltaTime);
}
void DestroyAfterTime()
{
Destroy(gameObject, lifeSpan);
}
}
Answer by Imdeeo · Jul 03, 2020 at 06:14 AM
Hi there! The problem is in the "GameObject.FindWithTag" in the Bullet script. This method search for one gameObject in the scene with the given tag and returns the first found. You can directly use the collision.gameObject to get the component.
collision.gameObject.GetComponent<MiniAsteroidHealth>();
Also, it's better to use OnTriggerEnter than OnCollisionEnter method for this kind of stuff because the first one is more efficient.
private void OnTriggerEnter(Collider other)
{
Instantiate(explosion, transform.position, transform.rotation);
if (other.CompareTag("Asteroid"))
{
asteroidHealthScript = other.GetComponent<AsteroidHealth>();
asteroidHealthScript.currentHealth -= bulletDamage;
}
if (collision.gameObject.tag == "MiniAsteroid")
{
miniAsteroidHealthScript = other.GetComponent<MiniAsteroidHealth>();
miniAsteroidHealthScript.currentHealth -= bulletDamage;
}
Destroy(gameObject);
}
I hope this helps you!
Your answer
Follow this Question
Related Questions
Destroy Instantiate is not working 0 Answers
How do i spawn an object with a button press? 3 Answers
Spawning only 1 gameObject 3 Answers