Issues With If Statements And Tags Part Of The Lesson

I had to work closely and deviate from the course material, using ChatGPT after the lesson on if statements and tags. To summarize the issues found in Delivery.cs :

The provided Unity C# script contains several problems that may cause compilation errors or unexpected behavior. The first issue is an incorrect class declaration, where the script defines two classes, Collision and Delivery , without proper structure. The Delivery class should be the only class in the script or placed in a separate file. Another critical issue is that OnTriggerEnter2D(Collider2D other) is mistakenly nested inside OnCollisionEnter2D(Collision2D other) , which is not valid in C#. Each method should be independent, and OnTriggerEnter2D must not be placed inside another function. Additionally, the OnCollisionEnter2D method is completely empty, meaning it serves no purpose. If it is meant to be included, it should at least contain a Debug.Log() statement for testing; otherwise, it should be removed to avoid confusion.

Another issue is the use of other.tag == "TagName" to compare object tags. While this works, the recommended approach is to use other.CompareTag("TagName") , which is optimized for performance and prevents potential errors if an object has no tag assigned.

The corrected version of the script ensures that OnTriggerEnter2D is properly structured as its own method, OnCollisionEnter2D is either implemented or removed, and CompareTag("TagName") is used instead of other.tag == "TagName" . These fixes will ensure the script follows best practices and functions correctly in Unity.

Here is an updated version of the code that actually works. I highly recommend that the course creator updates this lesson, as it caused a lot of confusion while trying to learn. Moving forward, I am concerned that future lessons may conflict with the corrections I have made. Unfortunately, this experience has led me to lose some trust in the course, as I shouldn’t have to rely on ChatGPT to correct fundamental mistakes in the material. Now, I feel like I don’t have as solid an understanding of how tags and collisions work as I should.

Here is the updated code that worked for me:

using UnityEngine;

public class Delivery : MonoBehaviour

{

void OnTriggerEnter2D(Collider2D other) // 🔥 FIXED: Changed Collision2D to Collider2D

{

    if (other.gameObject.CompareTag("PowerUp"))

    {

        Debug.Log("I am a PowerUp, I make you go zoom zoom");

    }

    else if (other.gameObject.CompareTag("Package"))

    {

        Debug.Log("I am a Package, thank you for picking me up <3");

    }

    else if (other.gameObject.CompareTag("Customer"))

    {

        Debug.Log("Hey! Where's My Package?");

    }

}

}

Where did you get the ‘provided’ code? There is nothing wrong with the course’s code.


Edit
Oh, I think I see your mistake; You copied the diff (difference)


This is a ‘diff’ - it only shows the differences between the original file and the new changes. The red parts are code that have been removed. The green parts are code that was added. This shows the before and after all smooshed into one file, and it also removes the bits that didn’t change. This is not the code you are looking for. These are only the changes.

To see what the file should look like after the changes, you can click here
image
This will take you to the file after the changes have been applied

Here is the code

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

public class Delivery : MonoBehaviour
{
    void OnCollisionEnter2D(Collision2D other) 
    {
        Debug.Log("Ouch!");
    }

    void OnTriggerEnter2D(Collider2D other) 
    {
        if (other.tag == "Package")
        {
            Debug.Log("Package picked up");
        }

        if (other.tag == "Customer")
        {
            Debug.Log("Package Delivered");
        }
    }
}

I agree with this, though…

Thank you! I apologize for any confusion and the intensity of my previous post. I was taken aback by the misalignment I felt when closely following the course material.

I tend to be quite an emotional person—on the bright side, that makes me highly creative, but it also means I struggle with understanding the complex logic of coding and figuring out how to achieve the final goal.

This is especially challenging when I get stuck in a loop where everything looks correct, yet it still isn’t working. That frustration can quickly spiral, making it even harder to troubleshoot.

I really appreciate the space and the feedback. I hope to continue progressing through the course and learning more. This form of game development unlocks an untapped creative potential in me, and I truly want to reach a point where there’s a fluid, dynamic connection between my passionate ideas and the technical knowledge needed to bring them to life in Unity.

Privacy & Terms