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
1
Question by Gilead7 · Apr 20, 2012 at 06:28 PM · c#functionreturn

code path returning error

I am working on a character generation script. Depending on the body type chosen, either petite, average or large, you get a stat bonus for dexterity or intelligence or strength. It all looks right, but I'm getting a BodyModifier(int, int, int, int): not all code paths return a value error.

 BodyModifier( _bodytype, _dexterity, _strength, _intelligence); //Call is here

 int BodyModifier( int _bodytype, int _dexterity, int _intelligence, int _strength)
 {
     _bodymodifier = 1;
     Debug.Log("Bodytype" + _bodytype);    
     Debug.Log("Dex=" + _dexterity);    
     
     if(_bodytype == 1)
     {
         Debug.Log("Previous Dex: " + _dexterity);
         _dexterity = _dexterity + _bodymodifier;
         Debug.Log("Dex is increased" + _bodytype + _dexterity);
         return _dexterity;
     }
     if(_bodytype == 2)
     {
         Debug.Log("Previous Int:" + _intelligence);
         _intelligence = _intelligence + _bodymodifier;
         Debug.Log(_bodytype + "Intelligence is increased" + _intelligence);
         return _intelligence;
     }
     if(_bodytype == 3)
     {
         Debug.Log("Previous Str" + _strength);
         _strength = _strength + _bodymodifier;
         Debug.Log(_bodytype + "Strength is increased" + _strength);    
         return _strength;
     }
 }
 

I did use the code sample button, please forgive me if it's not quite right. Can anyone tell me where the problem is? Thanks!

Comment
Add comment
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

2 Replies

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

Answer by rutter · Apr 20, 2012 at 07:08 PM

The compiler is performing some basic static analysis on your code. Only it's taking a very simplistic view, somewhat like this:

 int ReturnAnInt()
 {
     if (something) return 1;
     else if (somethingElse) return 2;
 }

What happens if both of those conditions fail? Do we still return a number? We promised to, so the compiler is a little confused.

Now, it could be that your program is written such that at least one of those conditions will always be true; the compiler has no simple way of knowing that, so it assumes it's possible for each branch to be taken (or not) and examines the outcome.

The simple way to resolve this is with some "failsafe" case that will probably never happen, but which satisfies the compiler:

 int ReturnAnInt()
 {
     if (something) return 1;
     else if (somethingElse) return 2;
     else return -2;
 }

Or:

 int ReturnAnInt()
 {
     if (something) return 1;
     else if (somethingElse) return 2;
     
     //this should never happen
     return -2;
 }

Or, if you're really confident:

 int ReturnAnInt()
 {
     if (something) return 1;
     else if (somethingElse) return 2;
     
     throw new System.InvalidOperationException();
 }

Any of the above should work. Pick your favorite?

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 Gilead7 · Apr 23, 2012 at 04:02 PM 0
Share

Thanks! I added else if statements and a final else at the end where I simply return 0.

avatar image
0

Answer by RustyShackleford · Apr 20, 2012 at 07:16 PM

Not all code paths return a value - you need to add something incase all the if statements fall through. If you're sure _bodytype will always be 1,2, or 3 just add a return 0; at the bottom of your function.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Function that returns instantiated object 1 Answer

Get enum value out of function c# 2 Answers

C# Return Type Error? 1 Answer

type function not returning. c# 2 Answers

Multiple Cars not working 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