Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my code in c# but it comes up as error CS1001: Identifier expected for the x, and error CS1519: Invalid token '=' in class, struct, or interface member declaration for = new Vector2. can someone please help?



C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Paddle : MonoBehaviour
{
public bool isPlayer1;
public float speed;
public rigidbody2D rb;

private float movement;

  

    // Update is called once per frame
    void Update()
    {
        
    {
        if (isPlayer1)
        {
        movement = Input.GetAxisRaw("Vertical");
        }
    }
    
    
    movement = Input.GetAxisRaw("Vertical2");
    }
    rb.velocity = new Vector2(rb.velocity.x, movement * speed);
    

}


What I have tried:

I have tried looking up and not found anything.
Posted
Updated 14-Nov-21 20:46pm
v2

1 solution

First off, indent your code properly:
C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Paddle : MonoBehaviour
{
    public bool isPlayer1;
    public float speed;
    public rigidbody2D rb;
    
    private float movement;
    // Update is called once per frame
    void Update()
    {
        {
            if (isPlayer1)
            {
                movement = Input.GetAxisRaw("Vertical");
            }
        }
        movement = Input.GetAxisRaw("Vertical2");
    }
    rb.velocity = new Vector2(rb.velocity.x, movement * speed);
}
It makes it a lot clearer to see what is actually happening!
And the first thing that is obvious is that there is a pair of redundant curly brackets in there:
C#
    void Update()
    {
        if (isPlayer1)
        {
            movement = Input.GetAxisRaw("Vertical");
        }
    movement = Input.GetAxisRaw("Vertical2");
    }
The second thing that becomes obvious is that this line of code:
C#
rb.velocity = new Vector2(rb.velocity.x, movement * speed);
Isn't inside any method.

That won't compile: you cannot execute code unless it is inside a method - there is no way to call it!

So start out by working out where that code should be, and your problems may start to go away...
 
Share this answer
 

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