So im building a little game in unity and made a grid system with the help of a youtube tutorial so heres the code with the bug
public class GridBuildingSystem : MonoBehaviour
{
private Grid<GridObject> grid;
private void Awake()
{
int gridWidth = 38;
int gridHeight = 38;
float cellSize = 1f;
grid = new Grid<GridObject>(gridWidth, gridHeight, cellSize, Vector3.zero,
(Grid<GridObject> g, int x, int y) => new GridObject(g, x, y));
}
public class GridObject
{
private Grid<GridObject> grid;
private int x;
private int y;
public GridObject(Grid<GridObject> grid, int x, int y)
{
this.grid = grid;
this.x = x;
this.y = y;
}
}}
I get a CS1593(Delegate 'del' does not take 'number' arguments) on (Grid g, int x, int y) => new GridObject(g, x, y) but the constructor takes 3 arguments.
Edit: Maybe it has todo with this:
public class Grid<TGridObject> : MonoBehaviour
{
private int width;
private int height;
private float cellSize;
private Vector3 originPosition;
private TGridObject[,] gridArray;
private TextMesh[,] debugTextArray;
public Grid(int width, int heigth, float cellSize, Vector3 originPosition, Func<TGridObject> createGridObject)
{
this.width = width;
this.height = heigth;
this.cellSize = cellSize;
this.originPosition = originPosition;
gridArray = new TGridObject[width, heigth];
debugTextArray = new TextMesh[width, heigth];
for(int x=0;x<gridArray.GetLength(0);x++)
{
for(int y=0;y<gridArray.GetLength(1);y++)
{
gridArray[x, y] = createGridObject();
}
}
}
}
Hope anyone can help and have a nice day :D
GridObject
is not a delegate, so it's not about that (your) constructor. – madreflectionGridObject
inclass Grid<GridObject>
is not theclass GridObject
you defined. There's a reason that type parameters, by convention, have aT
prefix. No real class will have that prefix so they can't be confused. Use that convention:class Grid<TGridObject>
or justclass Grid<T>
. – madreflectionFunc<GridObject> createGridObject
is a delegate with 0 arguments that returns aGridObject
. This method you're passing to it ((Grid<GridObject> g, int x, int y) => new GridObject(g, x, y)
) has 3 arguments and returns aGridObject
. Clearly they don't describe the same method. – DiplomacyNotWar