[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!

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

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 :wink: