collider.name not working properly for a specific object

Hello! so I am creating a 2D platformer, So I made a spike that kills the player. And Then I made another object that helps the player and then I made a winning flag thats supposed to start the next level but I havent made that so its supposed to say in console “YOU WIN!” but for some reason collider.name isnt working and is detecting the winning flag as a spike. And I cant figure out why.
Heres my code for it (this is in my character controller)

    private void OnTriggerEnter2D(Collider2D collider) {
            if (collider.name == "jump_pad") {
                Vector3 temp = PlayerStartPos.transform.position;
                temp.y = transform.position.y + 2;
                transform.position = temp;
            if (collider.name == "jump_pad_high") {
                Vector3 temmie = PlayerStartPos.transform.position;
                temmie.y = transform.position.y + 5;
                transform.position = temmie;
            }
            if(collider.name == "winning_flag") {
                Debug.Log("YOU WIN!!!!");
            }
            } else {
                RestartLevel();
               
            }
    }
    private void RestartLevel()
    {
        Debug.Log("Restarting level");
        Vector3 temp = PlayerStartPos.transform.position;
        temp.x = PlayerStartPos.transform.position.x;
        temp.y = PlayerStartPos.transform.position.y;
        temp.z = PlayerStartPos.transform.position.z;
        transform.position = temp;
    }

Its detecting the winning flag but it thinks its a spike even though I added a if statement that says if its a winning flag. The Winning Flag has a BoxCollider2D with IsTrigger on and has a RigidBody2D with the gravity scale set to 0. Sorry if the answer is extremely obvious I’m new to Unity

What does “isn’t working” mean?

Why not print out the name and see INSTANTLY what it is? Maybe it’s not even the right collider!

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

If your problem would benefit from in-scene or in-game visualization, consider using Debug.DrawRay() or Debug.DrawLine() to visualize things like raycasts or distances.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

It’s nothing to do with Unity but basic control flow. Your brackets/logic doesn’t make sense.

You do this:

if (collider.name == "jump_pad") {
                Vector3 temp = PlayerStartPos.transform.position;
                temp.y = transform.position.y + 2;
                transform.position = temp;

… but you don’t close the bracket so then you do this code only if the name is “jump_pad”:

if (collider.name == "jump_pad_high") {
                Vector3 temmie = PlayerStartPos.transform.position;
                temmie.y = transform.position.y + 5;
                transform.position = temmie;
            }

… but it cannot be both. You then do the same again then finally put in two close brackets!

You’re not being careful. Add in the closing bracket correctly for the first “jump_pad” test.

It’s funny that the first though is that Unity isn’t working somehow, in this case “collider.name” which is just “GameObject.name” which is a fundamental and simple piece that hasn’t changed in over a decade. :wink:

Oh ok I didn’t notice I missed a bracket. Sorry If I wasted your time.

No worries, you didn’t waste our time.

Personally it’s why I prefer to have the open/close brackets on the same indentation level but everyone likes it different! :slight_smile:

In my case there was a space at the end of the gameobject name :'D