Click here to Skip to main content
15,887,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i get this error (44,5):error CS0131: The left-hand side of an assignment must be a variable, property or indexer how to fix ?????????????

heres my code

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

public class WeaponScript : MonoBehaviour {

    public GameObject Weapon;
    public GameObject Postion;
    public AudioSource KnifeGrab;
    public bool KnifePickedUp;

    void Update()
    {

            if (Input.GetKeyDown("f")) 
            {
             KnifePickedUp = false;
             Postion.SetActive (false);
            }

             if (Input.GetKeyDown("f")) 
            {
             Weapon.SetActive (true);
             KnifePickedUp = true;
             KnifeGrab.Play();
            }

            if (Input.GetKeyDown("2")) 
            {
             Weapon.SetActive (true);
             KnifePickedUp = true;
             KnifeGrab.Play();
            }

            if (Input.GetKeyDown("1")) 
            {
             Weapon.SetActive (false);
             KnifePickedUp = false;
            }
    }

   public void playSoundEffect()
   {
if (Input.GetKeyDown("f") && KnifePickedUp = true)
       {
        KnifeGrab.Play();
       }
    }
}


What I have tried:

how to fix? i cant really understand what it means by the left-hand side of an assignment must be a variable, property or indexer
Posted
Updated 31-Dec-21 6:31am
v2

Try to replace:
C#
if (Input.GetKeyDown("f") && KnifePickedUp = true)

with
C#
if (Input.GetKeyDown("f") && KnifePickedUp == true)

or
C#
if (Input.GetKeyDown("f") && KnifePickedUp)
 
Share this answer
 
Quote:
i cant really understand what it means by the left-hand side of an assignment must be a variable, property or indexer

To add to what Patrice T has said, the error means that when you wrote this bit:
C#
Input.GetKeyDown("f") && KnifePickedUp = true
The system looks at it as an assignment because "=" is an assignment operator, not an equality comparison which would be "=="
And because the operator priority of an assignment is much higher than the logical AND operator "&&" it read it as
C#
(Input.GetKeyDown("f") && KnifePickedUp) = true
Not
C#
Input.GetKeyDown("f") && (KnifePickedUp = true)
As a result, it tried to assign a value to the result of the AND operation and gave you an error that it wasn;t a variable value.

As suggested, you could correct it by changing the assignment to a comparison:
C#
if (Input.GetKeyDown("f") && KnifePickedUp == true)
But since KnifePickedUp is a bool value anyway, it's a lot clearer to just use that directly:
C#
if (Input.GetKeyDown("f") && KnifePickedUp)
 
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