Ive got an error in unity from my code and dont know how to fix it

it says error is 22,31 error cs1001 identifier expected but i dont know what to do

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

[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour
{
    private Vector2 _input;
    private CharacterController _characterController;
    private Vector3 _direction; 

    [SerializeField] private float speed;

    private void Awake()
    {
        _characterController = GetComponent<CharacterController>();
    }

    private void Update()
    {
        var targetAngle;float = Mathf.Atan2(y:_direction.x, x:_direction.z) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(x:0.0f, y:targetAngle, z:0.0f);

        _characterController.Move(motion:_direction * speed * Time.deltaTime);
    }

    public void move(InputAction.CallbackContext context)
    {
        _input = context.ReadValue<Vector2>();
        _direction = new Vector3(-_input.x, y:0.0f, z:_input.y);
        _direction = transform.TransformDirection(_direction);
    }
}

The 22, 31 in the error tells you what line, and character in the script the error is on.

So look at line 22 and see what is wrong with the code. It should be pretty obvious.

i dont know im new to coding i justt watched a tutourial. i cant see any diffrence and ive already fixed 2 other erros

I can assure you the tutorial did not tell you to write line 22 in the script like that. Go back and double check what they did.

Right, this is why tutorials shouldn’t use Rider. The faded text isn’t actually code they’re written, it’s inserted by the text editor for convenience.

The actual code is just var targetAngle = Mathf.Atan2(_direction.x, _direction.z) * Mathf.Rad2Deg;

Though I wouldn’t use var for built in types, and write it float targetAngle instead to make this code more clear. This isn’t Java after all.

1 Like

thanks i fixed it in all of the code but now it says = is an invalid expression term
image

That is not like the code I wrote.

1 Like

ill just restart

even if that had been the required code, you have “targetAngle;float” not “targetAngle:float” the fact rider has shown you what type will be used is definately going to confuse new people, but in watching you would see they dont type the :float portion

No, you just need to copy the code I wrote.

Look at the two lines above one another for comparison, with yours on top, and mine below:

targetAngle; = Mathf.Atan2(_direction.x, _direction.z) * Mathf.Rad2Deg;
var targetAngle = Mathf.Atan2(_direction.x, _direction.z) * Mathf.Rad2Deg

Can you see the difference now? Code syntax needs to be 100% accurate.

1 Like

he types so fast i cant see anything he puts i just see what appears

Code is written quickly because the code editor auto-completes a lot of stuff for us. Nonetheless you can still pause the video and see what they’ve written.

If you’re having trouble with some random video on youtube, perhaps consider a more structured course: Junior Programmer Pathway - Unity Learn

its possible hes not typing that fast, but hes voiced over some spedup bit too… Even as someone with good typing skills, in that i can almost type as fast as people talk, I watch tutorials for fun, and even i cant keep up with half of them constantly having to start/stop/rewind a bit…

So…

Declaration of a variable syntax

type name = value;

now, the = value is optional

var (i personally avoid, cos you should know what type its going to be, but it is a personal choice) tells the compiler to pick what it thinks

so you can do like

int a;
var b = 23;

if you dont, it will complain… As has been pointed out, the compiler is pretty good at pointing out what it finds wrong. Occasonally its misleading, but it is actually pretty rare… so if it says line x, character y, chances are thats exactly where it goes wrong

1 Like