destroy ''fireball'' on collision

So i made this code using several codes found in unity questions, the script is attached to a billboard enemy (doom style)… this is the script the bullets seem like soap bubbles and they don’t destroy on collisions

using UnityEngine; using System.Collections;

public class S_skullTower : MonoBehaviour {

 public Transform target;
 public float turretSpeed;
 public float fireRate;
 public float fireBallHeight;
 public GameObject fireBall;
 public float range;
 float distance;
 private float _lastShotTime = float.MinValue;
 // Use this for initialization
 void Start () 
 {
     
 }
 
 // Update is called once per frame
 void Update () 
 {
     //Rotate turret to look at player.
     Vector3 relativePos = target.position - transform.position;
     Quaternion rotation = Quaternion.LookRotation(relativePos); 
     rotation.x=0;
     rotation.z=0;
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * turretSpeed);
     
     //Fire at player when in range.
     distance = Vector3.Distance(transform.position,target.position);
     
     if (distance < range && Time.time > _lastShotTime + (3.0f / fireRate)){
         _lastShotTime = Time.time;
         launchFireBall();
     }    
 }
 
 void launchFireBall()
 {
     Vector3 position = new Vector3(transform.position.x, transform.position.y + fireBallHeight, transform.position.z);
     Instantiate(fireBall, position, transform.rotation);
     Rigidbody rb = fireBall.GetComponent<Rigidbody> ();
     rb.velocity = transform.forward * 40;
 }
 
 void  OnTriggerEnter(Collider fireball)
 {
     if (fireBall.gameObject.tag == "Player") 
     {
         DestroyObject(fireBall.gameObject);
         Debug.Log("Bullet was destroyed");
     }
 }

In launchFireBall() u have Instantiated fireBall without assigning to any gameobject.

In OnTriggerEnter(Collider fireball)

check
fireball.gameObect.name before checking the fireBall.gameObject.tag

You have a logical error, your OnCollisionEnter function should not be in this script that is attached to the enemy.

If this script is attached to the enemy, the OnCollisionEnter will trigger whenever something hits the enemy, and you are checking if (player has hit enemy), then destroy colliding object (which is a player!).

Instead,

  • Remove the OnCollisionEnter function from this script

  • Make a new script and attach it to Fireball object (which will be instantiated when firing)

  • Make a new OnCollisionEnter function in that script and use this:

    void OnTriggerEnter(Collider other)
    {
    //If the fireball has collided with Player:
    if (other.gameObject.tag == “Player”)
    {
    //Write debug message first:
    Debug.Log(“Fireball was destroyed”);

           //Then, destroy the current instance of gameObject:
           Destroy(gameObject);
       }
    

    }
    That will make enemies fireballs disappear when they hit the enemy. (Alternative - maybe make it disappear when they hit anything?)

You will also want to make a similar script for whichever object your Player can fire at enemies, so that object is destroyed when it hits the enemy (and preferably destroys the enemy as well).