OnTriggerEnter recognizing object issue

Hello Again! I’ve been having problems for a few days now. I have a missile that collides with a target. I want it to react differently depending on different types of missiles OnTrigger.

When I use this code, it works (The target get’s shoved in the direction the missile came from)

  Vector3 obj_velocity;
   Vector3   impact_velocity;

   // Update is called once per frame
   void Update ()
   {

     impact_velocity.x = Mathf.Lerp (impact_velocity.x, 0, .05f);
     impact_velocity.y = Mathf.Lerp (impact_velocity.y, 0, .05f);
     impact_velocity.z = Mathf.Lerp (impact_velocity.z, 0, .05f);


     transform.Translate (obj_velocity + impact_velocity);
   }

   void OnTriggerEnter(Collider collision)
   {
     Debug.Log("BLAH Enter called." + collision.gameObject);
     Vector3 direction = collision.gameObject.transform.position - transform.position;
     impact_velocity = -direction / 4;
   }

However, when I try to make it recognize if the object hitting it is equal to a particular public GameObject in its’ inspector. It doesn’t think the object hitting it is the same.

  public GameObject my_missile_ref;

   Vector3 obj_velocity;
   Vector3   impact_velocity;
 
   // Update is called once per frame
   void Update ()
   {

     impact_velocity.x = Mathf.Lerp (impact_velocity.x, 0, .05f);
     impact_velocity.y = Mathf.Lerp (impact_velocity.y, 0, .05f);
     impact_velocity.z = Mathf.Lerp (impact_velocity.z, 0, .05f);


     transform.Translate (obj_velocity + impact_velocity);
   }

   void OnTriggerEnter(Collider collision)
   {
     Debug.Log("BLAH Enter called." + collision.gameObject);

     if (collision.gameObject == my_missile_ref)
     {
       Vector3 direction = collision.gameObject.transform.position - transform.position;

       impact_velocity = -direction / 4;
     }
   }

my_missile_ref is the object that In instantiate to shoot at the target. It is also the same object that I drag into this script’s inspector for the ‘public GameObject my_missile_ref’ variable.

But when I say if (collision.gameObject == my_missile_ref) it never goes onto the things inside the if statement.

Sorry for the long post, I really appreciate the help guys-

have you tried comparing it by tag?

Sounds like you’re trying to compare the instance with the prefab.

Can you post your instantiation code?

I actually had not tried to compare it by tag. And… it seems to work lol. So at least I know one way to do it, thank you!

Still Is there no way to compare the object directly? Here is my instantiation code:

using UnityEngine;
using System.Collections;

public class SF_FCS_missiles : MonoBehaviour
{
   public Object miss_prefab;

   void Update()
   {
     missile_script my_shot = Missile_fire(0, 0, Current_Missile_Side);
   }
   
   missile_script Missile_fire (float missile_direction_x, float missile_direction_y, GameObject launcher_origin)
   {   
     GameObject my_missile = Instantiate (miss_prefab, launcher_origin.transform.position, Jet_Camera_Rotator_Con.transform.rotation * Quaternion.Euler(new Vector3(missile_direction_x,missile_direction_y,0))) as GameObject;
     
     missile_script MISS = my_missile.GetComponent<missile_script>();
   
     return MISS;
   }
}

Aside from your naming conventions and prefab type, that all looks fine.

Where does my_missile_ref get assigned?