So I think this is my first posting here in a while, but I have an issue with my code for my rpg rocket.
So the issues stems from the fact that when I shoot and it touches anything it gets destroyed. However when i implement the code to deal damage to my enemies suddenly the rocket doesnt destroy itself? But it works when I delete that line of code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rpgRocketdestruction : MonoBehaviour
{
public LayerMask enemylayers;
public float explosionradius;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnDrawGizmosSelected()
{
Gizmos.DrawWireSphere(gameObject.transform.position, explosionradius); //for me to see the radius of the explosion
}
void OnCollisionEnter(Collision collision)
{
Collider[] enemiesinrange;
enemiesinrange = Physics.OverlapSphere(gameObject.transform.position, explosionradius, enemylayers); //adds anything with the enemylayers into array so only enemies
foreach (Collider enemy in enemiesinrange)
{
Debug.Log("we hit " + enemy.name);
enemy.GetComponent<Enemy>().TakeDamage(10);//the line that causes my issue
}
Destroy(gameObject);
}
}