The left-hand side of an assignment but be a variable, property or indexer

I looked it up and I saw a lot of people having the same problem. Unfortunately the fixes are not interchangeable, and what worked for them is not working for me.

I have a simply script to divide a variable by a certain amount and stores it into another variable, and each time x happens, the stored amount gets subtracted from the original variable. So if x happens the certain amount of times that the original variable was divided by, the original variable’s value will become 0. Unfortunately if the original variable’s value is an uneven number, I store the divided value into another float, it won’t have enough decimals to amount to 0, which is why I have to to use a decimal. Here is the script:

public float Speed = 6;
	private decimal SpeedSubstractionValue;
	
	private bool immobile;
	private int amountOfLegs;
	
	void Start () {
		foreach (Transform child in this.transform) 
		{
			if(child.name == "Leg")
				amountOfLegs++;		
		}	
		SpeedSubstractionValue = ((decimal)Speed / amountOfLegs);
	}

	public void TakeHit()
	{
		if(((decimal)Speed - SpeedSubstractionValue) >= 0)
		{
			(decimal)Speed = (decimal)Speed - SpeedSubstractionValue;
			if(Speed <= 0)
				immobile = true;
		}
	}
}

All this does is make so that when you hit all the legs on the object, the object’s speed will become 0. Unfortunately this line: (decimal)Speed = (decimal)Speed - SpeedSubstractionValue; gives me the error in my title.

I don’t understand your explanation of “Unfortunately if the original variable’s value is an uneven number, I store the divided value into another float, it won’t have enough decimals to amount to 0”, but the particular error message in question is occurring because you are trying to cast Speed as a decimal on the left hand side of an assignment. If you really must keep Speed as a float and SpeedSubstractionValue as a decimal, try replacing that line with:

Speed = Speed - (float)SpeedSubstractionValue;

However, I’d strongly encourage you to look through your code and try to get some consistency of what numeric types you’re using - all that (casting) is not good…

Can you consider changing both Speed and SpeedSubstractionValue to float OR decimal?

By doing so, you do not need to keep type-casting it to different variable types.