Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 RussellFincher · Jan 05, 2018 at 06:19 PM · functionfor-loopreturn value

Not all code paths return a value

I've got a function that is returning this error. I can't see where the path is terminating without returning a value. Maybe after numberOfCells++;, but shouldn't it just continue to the next iteration of the for loop?
.

Context: this function should be counting the number of empty cells in a rectangle with the bounds tempStartX, tempStartY and tempEndX, tempEndY by cycling through each cell and checking for empty cells. If it finds any non-empty cells it returns a zero (there is a reason for this). When it reaches the last cell it returns the number of cells. TileType is a 2D array storing the game object that is in each cell.

 public int NumberOfExecutableCells_SubModule (int tempStartX, int tempEndX, int tempStartY, int tempEndY){
 
 int numberOfCells = 0;

 for (int row = tempStartY; row > tempEndY ; row--){             // Cycle through rows
 for (int column = tempStartX; column < tempEndX; column++) {    // Cycle through columns

 if (TileType[column,row].name != "empty(Clone)"){               // If this cell is not empty
     return 0;                                                   // Return zero for # of empty cells
                 }
     else{                                                       // Otherwise if this cell is empty
         numberOfCells++;                                        // Add 1 to the # of empty cells
         if (row == tempEndY && column == tempEndX){             // If this is the last cell
             return numberOfCells;                               // Return the number of empty cells
                 }
             }
         }
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jan 05, 2018 at 06:32 PM

First of all this condition will never be true:

 if (row == tempEndY && column == tempEndX){

as your for loop conditions will terminate before you reach the end values. The actual issue is simply that a method with a return type always need to return a value. However even your for loops could not run at all depending on the start and end values. So it's possible that the for loops are skipped completely and you just reach the end of your function without returning anything.

In your case it seems to make sense to just remove the condition i've mentioned earlier and move the return statement to the end of the method.

 public int NumberOfExecutableCells_SubModule (int tempStartX, int tempEndX, int tempStartY, int tempEndY)
 {
     int numberOfCells = 0;
     for (int row = tempStartY; row > tempEndY ; row--)
     {
         for (int column = tempStartX; column < tempEndX; column++)
         {
             if (TileType[column,row].name != "empty(Clone)")
             {
                 return 0;
             }
             else
             {
                 numberOfCells++;
             }
         }
     }
     return numberOfCells;
 }




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

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

Loop a function, once per second, X number of times 1 Answer

IEnumerator Function 1 Answer

return value of a function passed to another function as a parameter 0 Answers

Choosing function with highest return value 2 Answers

Function cannot return int!? 2 Answers


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