Search Unity

Operator '&&' cannot be applied to operands of type

Discussion in 'Scripting' started by BeestGames, Oct 6, 2019.

  1. BeestGames

    BeestGames

    Joined:
    Oct 6, 2019
    Posts:
    14
    Hi all,

    This is my first post here. I'm following a course on Udemy and they have a pre-made game example to work with.

    The game is a 2d shooter. What I wanted to do was to make the enemy (which is falling down) warp to another x position when it goes off screen. I was successful in the warp, but the problem is that after the first warp, when the enemy goes off screen, it reappears at the same x position for the rest of consequent warps. Now what I'm trying to do is to make an if statement saying that "if the object gets to a specific x position & a specific y position it should warp to the determined position". However, unity dosn't allow me to use '&&'. Here is my script:

    void Update()
    {
    transform.Translate(Vector3.down * speed * Time.deltaTime);
    if (transform.position.y < -8)
    {
    transform.position = new Vector3(-6.69f, 7.85f, 0);
    }
    else (transform.position.y < -8 && transform.position.x == -6.69f);
    {
    transform.position = new Vector3(-4.69f, 7.85f, 0);
    }

    }
     
  2. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    "else" shouldn't be followed by a condition; it runs if all previous conditions failed to run. You want "else if" there.
    Also, don't put a semicolon at the end of the else line.
     
    BeestGames likes this.
  3. BeestGames

    BeestGames

    Joined:
    Oct 6, 2019
    Posts:
    14
    Thank you very much sir. It was fixed with your help.