Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Paparakas · Oct 30, 2013 at 08:55 PM · c#errordecimalassignment

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.

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dracorat · Oct 30, 2013 at 10:16 PM 0
Share

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".

avatar image Paparakas · Oct 30, 2013 at 10:21 PM 0
Share

@Dracorat Thanks for the explanation! That makes a lot of sense.

avatar image Paparakas · Oct 30, 2013 at 11:33 PM 0
Share

If I'm not understandable please tell me and I'll try to elaborate further.

avatar image Dracorat · Oct 30, 2013 at 11:35 PM 0
Share

(So take out the cast in the very front of the line and you'll be fine.)

avatar image Paparakas · Oct 30, 2013 at 11:44 PM 0
Share

@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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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....

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Paparakas · Oct 30, 2013 at 09:27 PM 0
Share

@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.

avatar image meat5000 ♦ · Oct 30, 2013 at 10:09 PM 0
Share

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Floor.html

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Ceil.html

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Round.html

avatar image meat5000 ♦ · Oct 30, 2013 at 11:43 PM 1
Share

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.

avatar image Dracorat · Oct 31, 2013 at 01:04 AM 1
Share

Doesn't that make more sense anyway?

avatar image meat5000 ♦ · Oct 31, 2013 at 11:05 AM 1
Share

Why are you counting legs in Decimals?? Use an INT!!!

Show more comments
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Paparakas · Oct 31, 2013 at 02:11 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Particle circle HELP 1 Answer

Missing Reference FPS help 1 Answer

Getting index of the smallest number in a list 1 Answer

NullReferenceException problem 2 Answers

error CS0122: `GameController.AddScore(int)' is inaccessible due to its protection level 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges