- Home /
onTriggerEnter is unclear to me...
Hey everyone,
I'm trying to use the onTriggerEnter function in a C# script placed on a parent Empty that will affect several gameObjects (namely gold coins one has to collect...)
I've tried reading the documentation but I haven't found (or understood) the information I'm looking for: how can I access the colliders of the Empty's children (ie each coin's collider) to make THEM react as triggers. The idea is, in pseudo-code:
for (int i=0; i<Empty'sChildren.Length; i++) {
onTriggerEnter(Collider Child[i].collider) {
Destroy child[i].gameObject;
score+=20;
}
}
My problem is I don't know where to put the for loop (because from what I understood onTriggerEnter is not to be put in the Update, so I can't include onTriggerEvent in a for loop), or if I don't use a for loop, how can I gain access to each of the children's colliders? I'm extremely new to Unity3D and C# AND a little lost, I must admit...
Thanks for your help,
Ari ;o)
Answer by fafase · Dec 11, 2013 at 09:44 AM
OnTriggerEnter is a callback method. It is not a method you call in a script, you simple implement it and it will be called by the engine when the appropriate situation occurs.
For instance OnTriggerEnter waits for an object to enter a collider marked as trigger.
Consider you have your player and there is a zone you want to use for an action, for instance in front of a door you place a cube collider and you want that when the player is inside that cube a pop up window shows up asking for the key to the door.
When the player enters the cube, the physic engine will detect the collision and see that the cube is a trigger so it will call the OnTriggerEnter method for all scripts attached to both objects. Logically, it makes more sense to place the script on the Cube.
So here is what you would have on your CubeScript.cs:
public class CubeScript :MonoBehaviour{
bool showGUI = false;
void OnTriggerEnter(Collider col){
if(col.gameObject.tag == "Player"){
showGUI = true;
}
}
void OnTriggerExit(Collider col){
if(col.gameObject.tag == "Player"){
showGUI = false;
}
}
void OnGUI(){
if(showGUI){
GUI.Box(new Rect(100,100,200,200), "You have the key?")
}
}
}
The col parameter is a reference to the other colliding object, somehow the one responsible for the call of the method. So we can use it to access the other object, in our case the player.
Answer by robhuhn · Dec 11, 2013 at 09:49 AM
OnTriggerEnter is called by messages from unity just like Start and Update (See Monobehaviour). So if you want to know when an object enters the trigger, add that method to a component placed on your trigger.
using UnityEngine;
using System.Collections;
public class GoldCoin : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
Debug.Log("object entered coin trigger");
}
}
Answer by wrstscrnnm6 · Dec 11, 2013 at 10:00 AM
onTriggerEnter() gets called automatically by Unity similarly to Start() and Update().
To use it you will want to create you coin object t should have a mesh (a sphere or cube will also work) component attached to it as well as a collider component. In the inspector, on the collider component you will see a check box labeled "Is Trigger" by checking this box you are telling Unity that you want this object to serve as a trigger volume rather than as a physical object.
You should create a (c#) script and attach it to the coin object. in that script you should put the following function:
void OnTriggerEnter(Collider other) {
//debug message that prints out the name of the object that collided with the trigger.
Debug.Log("triggered "+other.gameObject.name);
//whatever else you want to happen at the moment of collision should go here
}
here is a Javascript version of the script
function OnTriggerEnter (other : Collider) {
//debug message that prints out the name of the object that collided with the trigger.
Debug.Log("triggered "+other.gameObject.name);
//whatever else you want to happen at the moment of collision should go here
}
Now if you start your scene and run your character into the coin object you should notice two things:
Your character should pass through the coin object.
A message should pop up in your editor console.
You can add whatever other code you like into those functions.
Your answer
Follow this Question
Related Questions
Problem with prefab parent and children objects that need to be instantiated multiple times. 1 Answer
Parent/Player teleports but its children dont teleport aswell 4 Answers
sending a message to child objects 2 Answers
Distribute terrain in zones 3 Answers
Rotating/Aligning parents relative to their children 1 Answer