Search code examples
c#unity-game-enginegame-development2d-games

Game object does nothing on collide


guys! I'm new in game development and new in unity engine as well so maybe the question is stupid but i need to start the "Menu" scene when the ball collides with the platform but it does nothing. I have no idea about triggers and colliders. I've been reading about it for 2 hours now but still cannot solve the problem.

unity inspector settings

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

public class LoseCollider : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D col)
    {
        Debug.Log("hey " + col.name);
        SceneManager.LoadScene("Menu");
    }
}

Solution

  • Answer

    Check the Is Trigger box in the Collider component.

    Reasoning

    The reason you have to do this is because Unity has 2 collision "modes" - Normal collision (OnCollisionEnter) and Trigger collision (OnTriggerEnter). The difference between these two is that Normal collision checks for collisions and interacts with the object (no script), while Trigger Collision only checks for collisions and does not interact with the object. These 2 "modes" are not interchangeable - you cannot use the normal collision mode and check the Is trigger box (because Unity will only check for normal collision), and you cannot use the Trigger collision mode and not checked the Is Trigger box (Unity will only check for Trigger collision).

    Some Useful Links

    https://docs.unity3d.com/ScriptReference/Collider.html https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html

    Hope you find this useful :)