Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playermovement : MonoBehaviour
{ public CharacterController2D controller;

public float runSpeed = 40f;
   float HorizontalMove = 0f;

    // Update is called once per frame
    void Update()
    {
       HorizontalMove = Input.GetAxisRaw("Horizontal") = runSpeed;        
    }
     void FixedUpdate () 
    {          
        // Move our character
      controller.Move(HorizontalMove = Time.fixedDeltaTime, false, false);
    }
} 


What I have tried:

How can I fix that? Please help me to fix this 2D of trying and nothing changed.
Posted
Updated 30-Aug-23 13:32pm
v2

You can't assign a variable to a method.

C#
HorizontalMove = Input.GetAxisRaw("Horizontal") = runSpeed;

either;

C#
HorizontalMove = Input.GetAxisRaw("Horizontal");
HorizontalMove = runSpeed;

but not both.
 
Share this answer
 
In C# (and most if not all other languages) there is something called an LValue and an RValue: when you do an assignment you always have an LValue on the left of the equals sign and an RValue on the right.
The LValue is where the value is to be stored, and the RValue is the value to store into it.

The RValue can be pretty much anything: a constant, a variable, a property, a method return value: it's an expression that can be evaluated to a single item:
RValues
666
"Hello World"
Average(new List<int>() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
2 * Math.Pi * radius * radius
But an LValue is much more restrictive: it has to be an object that occupies some identifiable location in memory and which can be changed. In C# that means either a variable, a field, a Property that has a accessible setter (which looks like a variable) or an Indexer (basically something that can go between square brackets).

Your code tries to have two LValues:
C#
HorizontalMove = Input.GetAxisRaw("Horizontal") = runSpeed;
Which is a short form way to say:
C#
Input.GetAxisRaw("Horizontal") = runSpeed;
HorizontalMove = runSpeed;
HorizontalMove is a variable, specifically a float so that's a valid LValue.
runSpeed could be anything, but it's to the right of an equals sign, so that's OK - it's an RValue.
Input.GetAxisRaw("Horizontal") is a return value from a method call, so that again could be anything: a constant, a void, whatever - which makes it an RValue as well.

So your code is trying to assign the value of runSpeed to an RValue, and the system complains because you can't do that!
 
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