Click here to Skip to main content
15,851,242 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, My language is Spanish but I will try to speak in English so that they understand me.

I am learning to use unity through youtube videos and in the video that I see he writes a code that uses if, else if and else but, I put exactly the same as in the video and it does not let me, I get this "Assets \ Scripts \ PlayerMove.cs (34,10): error CS1525: Invalid expression term 'else' "

I have tried watching other videos but they all do the same and it does not work for me, can someone help me?


I leave the code for you to see the problem.

Unity 2020
Visual studio 2017
Win10

according to unity the error is in line 34,10

If someone helps me I would really appreciate it

What I have tried:

<pre lang="C#">


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

public class PlayerMove : MonoBehaviour
{

    //Variables
    public float runSpeed = 2;
    public float JupmSpeed = 3;

    Rigidbody2D rb2d;




    // Start is called before the first frame update
    void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    private void FixedUpdate()
    {
        if (Input.GetKey("d")) || Input.GetKey("right"))
        {
            rb2d.velocity = new Vector2(runSpeed, rb2d.velocity.y);
        }

        else if (Input.GetKey("a")) || Input.GetKey("left"))
        {
            rb2d.velocity = new Vector2(-runSpeed, rb2d.velocity.y);
        }
        else
        {
            rb2d.velocity = new Vector2(0, rb2d.velocity.y);
        }

    }
Posted
Updated 22-May-21 23:16pm

if (Input.GetKey("d")) || Input.GetKey("right"))


should be

if (Input.GetKey("d") || Input.GetKey("right"))
 
Share this answer
 
Comments
BillWoodruff 23-May-21 0:49am    
+5

As has already been stated, you have a syntax error in your nested if then else statements. My recommendation is to avoid such nested statements as they are error-prone and convoluted. Simply call Input.GetKey once and then call a method that uses a switch statement to return the vector. Something along these lines.


C#
private Vector2 ChooseVector(char key)
     {

         return key switch
         {
            ('d') or ('r') => new Vector2(runSpeed, rb2d.velocity.y),
            ('a') or ('l')  => new Vector2(-runSpeed, rb2d.velocity.y),
             _ =>  new Vector2(0, rb2d.velocity.y)
         };
     }
 
Share this answer
 
Comments
Richard Deeming 24-May-21 6:08am    
NB: The "switch statement" syntax is only supported in C# 8 or later. If you're targeting .NET Framework, or an earlier version of .NET Core, you will need to manually set the <LangVersion> in your project file to enable support. It's not officially supported in anything other than .NET Core 3.x or later.

Does C# 8 support the .NET Framework? - Stack Overflow[^]

I also doubt it would work in VS2017, which is what the OP is using.

And I believe the or pattern is part of C# 9. Again, you can enable it in VS2019 by manually editing your project file, but it's not officially supported, so many C# 9 features won't work.
George Swan 24-May-21 8:31am    
You are quite correct, Richard. The solution will compile with Net 5 and the default language version C#9.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900