Search Unity

Can't get triggers to work.

Discussion in '2D' started by SwiftyMove, Apr 30, 2016.

  1. SwiftyMove

    SwiftyMove

    Joined:
    Jul 9, 2013
    Posts:
    15
    I'm starting to make a game and I can't seem to get any triggers to work. What I've tried doing is once my player passes through a wall an object in the room becomes visible. I have set up the wall with a 2D Box Collider and have applied a rigid body on my player. My script is as follows:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Trigger : MonoBehaviour {
    5.  
    6.     public GameObject obj;
    7.    
    8.     // Update is called once per frame
    9.     void OnTriggerEnter2D() {
    10.         obj.SetActive (true);
    11.     }
    12. }
    Any help would be greatly appreciated!
     
  2. JayJennings

    JayJennings

    Joined:
    Jun 24, 2013
    Posts:
    184
    Make sure your player also has a collider2D object and make sure either the player's collider or the wall collider have the isTrigger box checked.

    Jay
     
  3. SwiftyMove

    SwiftyMove

    Joined:
    Jul 9, 2013
    Posts:
    15
    Thank you for the reply, but neither of these seem to be the problem :(
     
  4. JayJennings

    JayJennings

    Joined:
    Jun 24, 2013
    Posts:
    184
    What is your Trigger script attached to?

    Also, throw a Debug.Log("Triggered!"); into the OnTriggerEnter2D() function -- that way you'll know whether the problem is with the triggering or with the obj being set to active. (If the message shows, the trigger works.)

    Jay

    PS - I would imagine Unity would throw an error if it cares, but you might want to include the parameter that's passed into the OnTriggerEnter2D function:
    void OnTriggerEnter2D(Collider2D other)
     
  5. SwiftyMove

    SwiftyMove

    Joined:
    Jul 9, 2013
    Posts:
    15
    I tried this, and the Debug.Log doesn't even show in the console. :/