Home > Back-end >  OnCollisionEnter2D is not being called
OnCollisionEnter2D is not being called

Time:08-11

I'm fairly new to Unity and I've decided to work on a simple 2d platformer from youtube tutorials. Everything works fine until I start using collisions. My problem isn't only that it doesn't work, It's that it doesn't appear at all on auto-complete. Here is my code:

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

public class finishLine : MonoBehaviour
{

    void Start()
    {
    
    }


    void Update()
    {
    
    }

    private void OnCollisionEnter2D(Collision2D col) {
        if (col.gameObject.CompareTag("Player")) {
            SceneManager.LoadScene("level2");  
        }
    }
}

I've also added rigid bodies and box colliders to both objects (player and finish line). Anyone know what's wrong?

CodePudding user response:

Did you add a tag to the object? to see the objects that collided, add before the condition - print(col.gameObject.tag); and mb print(col.gameObject.name); for check work

CodePudding user response:

Did you attach the script to the object the player is supposed to collide with? Also, you may have forgotten to put a tag on the player for the "finish line" to collide with. Try those, it might work.

CodePudding user response:

Make sure that you used BoxCollider2D and not the 3D one. If the autocomplete doesn't work try restarting VisualStudio or whatever IDE you use. If that doesn't work look in the Unity Settings under External Tools and try regenerating project files. That works for me sometimes.

  • Related