Lap checkpoints

Hi dudes,

Following a basic checkpoing tutorial

its for 3d and using OnTriggerEnter()

but im using 2d (Which i wouldnt think makes a difference)

my car isnt being detected by the collider
Any ideas why the collision detection isnt working?

here is the code:

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

public class LapCheckpoint : MonoBehaviour
{
    public int Index;

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.GetComponent<LapController>())
        {
            print("car detected");
            LapController car = collision.GetComponent<LapController>();

            if (car.checkpointIndex == Index + 1 || car.checkpointIndex == Index -1)
            {
                car.checkpointIndex = Index;
                print(Index);
            }
        }
    }
}

oh uh lapController is a script attached to my car.

Assuming your car is using a Rigidbody2D for movement, make sure the LapController component is on the same GameObject as the Rigidbody2D component, as that’s the GameObject that will be detected with triggers/collisions.

Also, just one minor optimization to make in this script - you’re calling GetComponent<LapController>() twice, when you should really only do so once:

private void OnTriggerEnter2D(Collider2D collision)
{
  LapController car = collision.GetComponent<LapController>();

  if (car != null)
  {
    print("car detected");
    if (car.checkpointIndex == Index + 1 || car.checkpointIndex == Index -1)
    {
        car.checkpointIndex = Index;
        print(Index);
    }
  }
}

Hi Vryken thanks for your reply, really apppreciate it!

i have gone over the tutorial again and have everything perfectly setup as it is. But the collision just isnt working.

i have a LapStart object with a box 2d trigger collider component which has this scrip attached to it:
in the inspector i have set the amount of checkpoints there are (3) to the checkpointAmt in the inspector.

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

public class LapHandle : MonoBehaviour
{
    public int checkpointAmt;

    private void OnTriggerEnter2D(Collider2D collision)
    {
       
        if (collision.GetComponent<CarLap>())
        {
            CarLap car = collision.GetComponent<CarLap>();

            if (car.checkpointIndex == checkpointAmt)
            {
                car.checkpointIndex = 0;
                car.lapNumber++;
                print(car.lapNumber);
            }
        }
    }
}

Then I have 3 checkpoints which are named checkpoint 1 to 3 with box2d triggers on them. They have this script:
and i have there indexes in the inspector set from 1 to 3

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

public class LapCheckPoint : MonoBehaviour
{
    public int index;

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.GetComponent<CarLap>())
        {
            CarLap car = collision.GetComponent<CarLap>();

            if (car.checkpointIndex == index + 1 || car.checkpointIndex == -1)
            {
                car.checkpointIndex = index;
                print(index);
            }
        }
    }
}

and finally on my car, where the rigidbody2d is attached i have this script:

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

public class CarLap : MonoBehaviour
{
    public int lapNumber;
    public int checkpointIndex;

    private void Start()
    {
        lapNumber = 1;
        checkpointIndex = 0;
    }
}

Again thanks for the help!