Trouble Activating a Trigger on Unity

Hello again, I’m following the Unity 2D course and am having trouble activating a trigger. I wrote the code as in the video, checked “Is Trigger” on the object, added the trigger script to the object as a component, and wrote a Debug.Log message to be displayed in the Console window when I pass through the object. However, even though I did all of these things, the trigger does not work because I every time I go through the object nothing appears on the Console window. I’m not sure what the problem is. Maybe it has to do with coding? Here is a picture of the code I wrote for the trigger. Can you help me find out what the problem is?

Thanks

Here’s another trigger script that I’m having problems with:

You have all the methods inside the start method. The should not be in the start method. Take them out

Ok, I took the code out of the Start Method but it still seems to be giving me problems. Now I’m having compiling errors and I can’t figure out what’s wrong. Here’s what the code looks like now:

OnCollisionEnter is for 3D, so don’t use it if you are doing a 2D game.
Then, OnTriggerEnter2D is broken. It’s just the function name, then an if. Your function need to be enclosed in { }

void OnTriggerEnter2D(Collider2D other)
{ //<-- These
    if (other.tag == "Package")
    {
        Debug.Log("Livraison effectue");
    }
} //<-- These

Ok, I did what you said and changed it to OnTriggerEnter2D. I also tried applying more {} to the code. However, it’s still giving me an error message. I’m new when it comes to coding. I know it’s something simple but I can’t seem to figure it out. Here is how the code looks now:

Everything looks fine here now. Except that you didn’t need more {} at OnCollisionEnter2D, but it will still work.
What is the error message you are seeing? And can you post the full class. Don’t paste a screenshot, copy the code. You can see how to format code in this post: How to apply code formatting within your post

Ok, here is the complete code:

public class Delivery : MonoBehaviour

{

    void OnCollisionEnter2D(Collision2D other)

       

    {

    {

        Debug.Log("Gramerci");

   

    }

   

    }  

    void OnTriggerEnter2D(Collider2D other)

{

   

    if (other.tag == "Package")

    {

        Debug.Log("Livraison effectue");

    }

                   

}

Also the error message is Assets\Delivery.cs(33,6):errorCS1513:}expected

Your class is missing the last }
Every { must have a matching }

This is your class (formatted)

public class Delivery : MonoBehaviour
{ // d ?? no matching }
    void OnCollisionEnter2D(Collision2D other)
    { // a
        Debug.Log("Gramerci");
    } // a

    void OnTriggerEnter2D(Collider2D other)
    { // b
        if (other.tag == "Package")
        { // c
            Debug.Log("Livraison effectue");
        } // c
    } // b

That did the trick! However, it’s not time to celebrate yet becaue I have another problem. I made sure to save my level everytime I logged into Unity. However, when I opened the level everything I had on my windows and hierarchy were completely erased! All I had were the scripts and the assets I put in my assets folder. How did that happen? Is there a way to reverse it? And, if not, what do I do to make sure it does not happen again?

Check that Unity didn’t load a blank scene. If it did, your scene will still be where you saved it and you can load it. Else, you may just have to create it again. I don’t think I’ve ever experienced this, so I don’t know what else to tell you

I understand. I keep loading the project and it keeps giving me the same blank scene. Unfortunately, the scene I want to load is not available to load. It seems like I’ll have to do it over again. It’s ok though. Hopefully it won’t happen again :crossed_fingers:

Thanks for helping me solve the issue I had with coding the trigger. At least the C# scripts I made were salvaged.

Hang on! After some investigation I found the scene I was supposed to load. Next time, I’ll do a better job naming my scenes. Thanks so much for you help! I appreciate it. :grinning:

1 Like

Privacy & Terms