- Home /
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.
You started that line with a cast when you typed (decimal)
- when you cast a variable, it becomes a value, not a variable. Since now there's a value left over, the IDE is saying "you can't have a line with a value left over, you would have to assign that value to something".
@Dracorat Thanks for the explanation! That makes a lot of sense.
If I'm not understandable please tell me and I'll try to elaborate further.
(So take out the cast in the very front of the line and you'll be fine.)
@Dracorat But the Speed variable is a float, so it still doesn't work since you're adding up two decimals and attempting to assign it to a float.
Answer by tanoshimi · Oct 30, 2013 at 09:05 PM
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....
@tanoshimi It didn't work :( Basically, to explain the quote: Say you have a float variable named "Speed" with the value "2". Now say you wanted to take off a certain portion of that value everytime x happens, and when x happens 6 times, the value is supposed to be "0". So to take off an even portion everytime x happens, I do 2/6 which gives me the number 0.333333.... If I use float, I won't get all the decimals I need because float would be 0.33333 . While decimal would be 0.3333333.... 6 0.3333333... = 2 But 6 0.33333 = 1.99998 which is not the whole number. So if I substract the float, eventually I'll end up with 0.000002 , yet if I substract the decimal I'll get exactly 0.
When dealing with float you will find from time to time, you will have to make it fit. Unity introduces error all on its own so using ranges and rounding occasionally is a necessity. Don't round during a calculation, but round at the end if you are within a margin or error. For example, if you are decrementing a float by some decimal you may have to use if( t < 0 )
ins$$anonymous$$d of if( t == 0 )
as a basic example. $$anonymous$$ajority of the time, hitting 0 exactly just won't happen.
Answer by eskimojoe · Oct 31, 2013 at 01:38 AM
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.
Unfortunately that's not an option as changing Speed to a decimal would require even more typecasting. Nonetheless, I've already solved the problem by simply tracking the legs left.