How do you trigger a specific collider using OnTriggerEnter?

Hey, guys, I’m new to unity, and C# in general, but I am wanting to have a prefab, which has 2 colliders; use one for general detection of triggers, and one specifically for the use of another prefab’s OnTriggerEnter. Any help is welcome, and I hope to hear from you soon!


void OnTriggerEnter(Collider particles)
{
    Flight();
    //Destroy(gameObject);
}

HEY,

Okay so you want one to only collide with certain objects?

The bellow script will allow the gameobject with the script attached to ignore objects with a certain tag.

void OnCollisionEnter(Collision collision)
{
        if (collision.gameObject.tag == "WHATEVER")
        {
            Physics.IgnoreCollision(collision.collider, GetComponent<Collider>());
        }
      }
    }

Hope this helps

@MS77Lieger Hi , you should use the onCollisionEnter method, you can use several properties to make shure you can trigger the exact collider you want, for this method to work you need to set correctly the properties of the object you want to collide with… first you need to make shure that the check mark in the Collider Properties (inspector window) that says “IsTrigger” is checked ,this is so the trigger method can detect it…When this is checked objects can go thru this collider not hiting with him, so you may need to put two colliders on the object makin the “IsTrigger” one bigger than the normal one so the system can detect the collider… Next you need to decide on how you are going to detect the collision, this can be with “GameTag” “Name” or others… @Goldiedog123 gave an example using “Gametag” we will use the “Name” of the game object.

 void OnCollisionEnter(Collision dataFromCollision)
 {
         if (dataFromCollision.gameObject.name == "GameObjectName")
         {
            //Do whatever you want 
         }
       }
     }

Look that when you get the data from collision is in a “Collider” type and you acces the gameObject properties of this collider… one of the game object properties is “name” others are “CompareTag” ,“GetComponent”, “GetComponentInChildren” and so on

you can also acces other properties of the collider itself

Vector3 triggerPosition = new Vector3 (1.0f,2.3f,4.3f); // Storing some position in space

	void OnCollisionEnter(Collision dataFromCollision)
	{


	if (dataFromCollision.transform == triggerPosition){
		//Not very useful but here it does something when you collide with something in this exact triggerPosition
	}
}

I truly recomend you to use this method, is one of unity´s most powerful tools.
you can activate a scene when we pass thru a collider or maybe make an explosion, a jump scare, a portal when we hit a door with a special tag, a respawn if you get out of level… etc

he asked for an Ontriggerenter not oncollisionenter

I create a sphere with two colliders: one collisionEnter, other ColliderEnter, added a rigidBody. A function for eachother, like danger an safe zones. In the safe zone i used: TriggercolliderEnter, I used isTrigger event, but in the danger zone I used CollisionEnter, it works perfectly. thanks!

Try This:

public class ScriptOnTrigger : MonoBehaviour
{
public Collider OtherCollider01 = null;

void OnTriggerEnter(Collider other)
{
   if (other.gameObject.name == OtherCollider01.name)
	
	{
   
	 //Do whatever you want 	
	
	}
			
}

,Try This:

public class ScriptOnTrigger : MonoBehaviour
{
public Collider OtherCollider01 = null;

void OnTriggerEnter(Collider other)
{
   if (other.gameObject.name == OtherCollider01.name)
	
	{
   
	 //Do whatever you want 	
	
	}
			
}