Search Unity

  1. Tune in to get all the latest Unity news live from Berlin on June 19, 6pm CEST. Set up a YouTube reminder!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  7. 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 at 11:41 PM.

  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 at 11:53 PM
  2. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    102
    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?