Home > Net >  Why doesn't this code for collision work in unity Mobile 2D
Why doesn't this code for collision work in unity Mobile 2D

Time:10-04

This code is attached to my player by a script named PlayerCollision:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCollision : MonoBehaviour
{
    void Start()
    {
        
    }

    void Update()
    {
        
    }

    void OnCollisionEnter (Collision collision)
    {
        if (collision.collider.tag == "Finish")
        {
            Debug.Log("Level Finished");
        }
    }
}

Can Someone tell me why this code is not displaying Level Finished in the Console

CodePudding user response:

If it's a 2D game, I think you want to use OnCollisionEnter2D: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter2D.html


    void OnCollisionEnter2D (Collision2D collision)
    {
        if (collision.collider.tag == "Finish")
        {
            Debug.Log("Level Finished");
        }
    }
  • Related