Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

2D triggers not working Unity 4

Discussion in 'Scripting' started by Gurdev, Jun 14, 2018.

  1. Gurdev

    Gurdev

    Joined:
    Jul 19, 2017
    Posts:
    2
    Hi everyone, I am as new as it gets to Unity and I can't get this simple trigger collision to work.

    I have a game with a player and a key, and I just want the player to be able to pick up the key when they get to it, but it looks like I can't even register the collision in the first place.

    Anytime I move my Player over to the key, nothing is logged to the console.

    PlayerController script on the Player object

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.  
    6.     public float speed = 5.0f;
    7.  
    8.     void Start () {
    9.     }
    10.  
    11.     void Update () {
    12.         if (Input.GetKey(KeyCode.LeftArrow)) {
    13.             transform.position += Vector3.left * speed * Time.deltaTime;
    14.         } else if (Input.GetKey (KeyCode.RightArrow)) {
    15.             transform.position += Vector3.right * speed * Time.deltaTime;
    16.         } else if (Input.GetKey(KeyCode.UpArrow)) {
    17.             transform.position += Vector3.up * speed * Time.deltaTime;
    18.         } else if (Input.GetKey(KeyCode.DownArrow)) {
    19.             transform.position += Vector3.down * speed * Time.deltaTime;
    20.         }
    21.     }
    22. }
    23.  
    Collectible script on the Key object

    Code (CSharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5. public class Collectible : MonoBehaviour {
    6.     void Start () {
    7.  
    8.     }
    9.  
    10.     void Update () {
    11.  
    12.     }
    13.  
    14.     void onTriggerEnter2D(Collider2D trigger) {
    15.         Debug.Log ("trigger");
    16.     }
    17. }
    18.  
    Inspector view for each object:

    upload_2018-6-14_18-37-19.png
    upload_2018-6-14_18-38-35.png


    Any help would be greatly appreciated. Thanks in advance :)
     
    Last edited: Jun 14, 2018
  2. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    111
    Collectible script should be on the player.
    Also it's not "onTriggerEnter2D" but "OnTriggerEnter2D".
     
  3. Gurdev

    Gurdev

    Joined:
    Jul 19, 2017
    Posts:
    2
    Thank you! I used your second suggestion and it works well.

    Why do you suggest putting the script on the player, though?