0 votes
by (8.2k points)
edited by

I'm trying to make a small program that control the movement of the player with C# project.

I want to access the variables of my class 'PlayerController' from the class 'PlayerController' but I keep getting this error: The left-hand side of an assignment must be a variable. Anyone can explain to me what am I am doing wrong here and how to avoid such mistake in the future?

Error:- [22:36:52) Assets\Scripts\PlayerController.cs(17,28): error CS0131: The left-hand side of an assignment must be a variable, property or indexer.

Script which I Used :- 

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


 

public class PlayerController : MonoBehaviour

{

    CharacterController characterController;

    public float MovementSpeed = 1;

    public float Gravity = 9.8f;

    private float velocity = 0;


 

    private void Start()

    {

        characterController = GetComponent<CharacterController>();

    }

    void Update()

    {

        // player movement - forward, backward, left, right

        float horizontal = Input.GetAxis("Horizontal") * MovementSpeed;

        float vertical = Input.GetAxis("Vertical") = MovementSpeed;

        characterController.Move((transform.right * horizontal + transform.forward

* vertical) * Time.deltaTime);


 

        // Gravity

        if (characterController.isGrounded)

        {

            velocity = 0;

        }

        else

        {

            velocity -= Gravity * Time.deltaTime;

            characterController.Move(new Vector3(0, velocity, 0));

        }

    }

}

1 Answer

0 votes
by (8.2k points)
  • In an assignment statement, the value of the right-hand side is assigned to the left-hand side. The left-hand side must be a variable, property, or indexer.
  • To fix this error, make sure that all operators are on the right-hand side and that the left-hand side is a variable, property, or indexer.
In Line you have used 
  • float vertical = Input.GetAxis("Vertical") = MovementSpeed;
Please remove "=" and then Use the correct operater.
  •  float vertical = Input.GetAxis("Vertical") * MovementSpeed;
TechXR runs courses in AR / VR / Metaverse / Game Development. Some of the popular Q&A in our courses are available here for easy discovery.

113 questions

117 answers

8 comments

99.7k users

...