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 /
  • Help Room /
avatar image
0
Question by TheBlackBox · Aug 23, 2015 at 10:07 PM · c#errorcontrollerreturn valuepaths

[C#] Not all code paths return a value

Hey there,

So I'm working on some random dungeon-gen, and up until now it's being going great.

I am getting the error Not all code paths return a value I looked into the error, tweaked my code a little and I'm still not making any progress. Let me post my code.

 using UnityEngine;
 using System.Collections.Generic;
 
 public class DTileMap 
 {
     protected class DRoom 
     {
         public int left;
         public int top;
         public int width;
         public int height;
 
         public int right {
             get {return left + width - 1;}
         }
 
         public int bottom {
             get { return top + height - 1; }
         }
 
         public bool CollidesWith(DRoom other) {
             if( left > other.right-1 )
                 return false;
             
             if( top > other.bottom-1 )
                 return false;
             
             if( right < other.left+1 )
                 return false;
             
             if( bottom < other.top+1 )
                 return false;
             
             return true;
         }
 
     }
     
     int size_x;
     int size_y;
 
     int[,] map_data;
 
     List<DRoom> rooms;
 
     public DTileMap (int size_x, int size_y)
     {
         this.size_x = size_x;
         this.size_y = size_y;
 
         rooms = new List<DRoom> ();
 
         map_data = new int[size_x, size_y]; 
         for (int i=0; i < 30; i++) {
             int rsx = Random.Range (4,8);
             int rsy = Random.Range (4,8);
 
             DRoom r = new DRoom();
             r.left = Random.Range(0, size_x - rsx);
             r.top = Random.Range(0, size_y-rsy);
             r.width = rsx;
             r.height = rsy;
 
             if(!RoomCollides(r)){
                 rooms.Add (r);
                 MakeRoom(r);
             }
         }
     }
 
     bool RoomCollides(DRoom r)
     {
         foreach (DRoom r2 in rooms)
         {
             if(r.CollidesWith(r2))
             {
                 return true;
             }
         }
     }
 
     public int GetTileAt(int x, int y){
         return map_data [x, y];
     }
 
     void MakeRoom(DRoom r)
     {
         for (int x=0; x < r.width; x++)
         {
             for (int y=0; y < r.height; y++)
             {
                 if(x==0 || x == r.width -1 || y==0 || y==r.height -1)
                 {
                     map_data[r.left+x, r.top+y] = 3;
                 } 
                     else 
                 
                     {
                         map_data [r.left + x, r.top + y] = 1;
                     }
             }
         }
     }
 }
 
 // Id's
 // 0 = Void
 // 1 = Floor
 // 2 = Random
 // 3 = Wall

Now, the error code is pointing towards line 69, but I check all of my value on lines 21 - 34.

If you need any extra information, let me know. I've tried to figure this out, but I'm stumped...

Thanks in advance!

Comment
Add comment · Show 4
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 Positive7 · Aug 23, 2015 at 10:27 PM 0
Share

You missing a " return " in DTile$$anonymous$$ap method

return null; return size_x; return whatever;

avatar image TheBlackBox · Aug 23, 2015 at 10:35 PM 0
Share

@Positve7 this was close! But thanks to your answer I realised how blind I was, I've figured it out. I'll post the answer just in case others have the same problem. Thanks for your help!

avatar image Positive7 · Aug 23, 2015 at 10:39 PM 0
Share

Also you got the class name and method with same name "DTile$$anonymous$$ap" that could be the problem too

avatar image pavlito · Mar 10, 2017 at 10:04 AM 0
Share

The function DTile$$anonymous$$ap doesn't need to explicitly return anything. It's a constructor. The return type and function name are the same as class name.

2 Replies

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

Answer by TheBlackBox · Aug 23, 2015 at 10:38 PM

Thanks to @Positive7 I managed to find the answer, and now I feel like an idiot...

This is the code snippet where the answer lay...

      bool RoomCollides(DRoom r)
      {
          foreach (DRoom r2 in rooms)
          {
              if(r.CollidesWith(r2))
              {
                  return true;
              }
          }

This is the code, fixed...

     bool RoomCollides(DRoom r)
     {
         foreach (DRoom r2 in rooms)
         {
             if(r.CollidesWith(r2))
             {
                 return true;
             }
         }
 
         return false;


And now, I feel like an idiot...

Thanks for your help @positive7

Comment
Add comment · Show 2 · 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 Positive7 · Aug 23, 2015 at 10:45 PM 0
Share

Hehh, interesting ... DTile$$anonymous$$ap should return something too cause its not void , but your class name is DTile$$anonymous$$ap too, As far as I Remember that's not good. I always avoid it so I might be wrong. Glad you fixedd it. $$anonymous$$eep up the good work!

avatar image pavlito · Mar 10, 2017 at 10:04 AM 0
Share

The function DTile$$anonymous$$ap doesn't need to explicitly return anything. It's a constructor. The return type and function name are the same as class name.

avatar image
-1

Answer by ronaldosilva · Aug 23, 2015 at 10:39 PM

Hi! Looks like the problem is that the function public DTileMap (int size_x, int size_y) is not marked as void for the return type. So the compiler is waiting for something to be returned from this function. Try to put void as the return type of the function as you did in other places and it should be fixed ;)

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 pavlito · Mar 10, 2017 at 10:06 AM 0
Share

That function is a constructor. It implicitly returns the object of the its class (type).

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

28 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

Related Questions

How do I reference an incremented int with one button, that was incremented from another button? 1 Answer

The body of '' cannot be an iterator block because 'void' is not an iterator interface type??? 2 Answers

Coding Errors 1 Answer

A namespace cannot directly contain mambers such as fields or methods... 2 Answers

Flashlight script help! 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