• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ari-Bouaniche · Dec 11, 2013 at 09:37 AM · c#colliderparentchildren

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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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");
     }
 }




Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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:

  1. Your character should pass through the coin object.

  2. A message should pop up in your editor console.

You can add whatever other code you like into those functions.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotating/Aligning parents relative to their children 1 Answer

Script to add script to all children 1 Answer

Deactivate Parent AND Children (C#) 1 Answer

Get Children of Child and Deactivate/Activate on Demand 1 Answer

How to move the parent object to a child of one of its children? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges