2d Platformer - Tags not working and for else script error CS1513 } expected.

Hello Guys,

iam new to programming and i searched web and youtube for some answers, followed tutorial but somehow i get two issues.

I got a very easy script which, on collision, will change the scene to the game over scene. So far it worked but now i implemented flying enemys which go down diagonally and enter the killbox i have set up below my platforms.

So when they touch the killbox the game ends too, which is not as intended as it should only happen when the player touches is.

Problems:
FIRST: The Tags are not working, it does not care if i use the TAG “Player” or “Enemy”
SECOND: I thought of using an else block for doing nothing but everytime i do that it gives the error CS1513 } expected while i have set up all the brackets.

Again iam really new to programming, so i hope you can help me with this maybe easy problem.

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

public class TrapScript : MonoBehaviour
{

void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == “Player”);
{
SceneManager.LoadScene(“GameOver”);
}
else
{
// do nothing
}
}
}

Remove “;” at the end of if (other.tag == “Player”);

Hello iMobCoding!

Thanks a bunch, why did the unity give me not that kinda error.

Maybe one more question i noticed now as it works. I try it out right now but maybe someone can give me a quick tip.

I want if the Enemys touch the killbox, that these enemys get destroyed as game object so it does not get overloaded over time. I think i will need to ork with “Destroy (gameObject)”?

Thanks again for solving my issue :slight_smile:

EDIT: Okay found the option to do so :slight_smile: - Closed

Human coders are able to work out the intention of a common piece of code, but compilers can’t. They can only do what they are specifically told to do, and no more.

It didn’t tell you that you shouldn’t put a semicolon after an if statement because putting a semicolon after an if statement is legal syntax. This:

void Update() {
if (x > 5);
}

Compiles. It evaluates whether x is greater than 5, and then does nothing with that result. (I’m not sure why this is valid syntax, but there may be a use for it. Maybe it’s so that coders can take advantage of short-circuiting chained boolean code without needing to do anything with the result? The important thing is that it is valid syntax, but it doesn’t act like the semicolon isn’t there.)

Similarly, a pair of { } can be put around any code. Though they’re most often used with keywords like “if”, “while”, “for”, etc, they are perfectly valid with or without an accompanying keyword. (This is sometimes useful for limiting the scope of certain variables, for example.)

So as far as the compiler is concerned, so far, it has seen an if statement, which ends, and then some code inside { }, and those two things are not related (despite your intentions as a coder) because of the semicolon in between them. So far nothing is invalid code! (even though it won’t work as intended)

Then the compiler gets to the “else”, and now it sees an actual problem: “else” requires an “if” statement before it. so it looks at the { } bracketed code, and looks before it for an if statement, but because the { } isn’t connected to the if statement, it can’t find one. So, that’s the error the compiler gives you.

Hello Star Manta,

thanks so much for the explanation about the if and else. I was thinking something in that lines but didnt really understand why that exact error message came up. But to see it as two different parts because of the semicolon ends it is reasonable. Thanks a lot for that big info for such a small error i made as a novice. Really helpful.

if (x > 5); compiler sees as

if (x > 5)
{
    ;
}

Which means, if (x > 5) do nothing