Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question "The non-generic type 'Grid' cannot be used with type arguments" error

Discussion in 'Scripting' started by puddleglum, Dec 1, 2023.

  1. puddleglum

    puddleglum

    Joined:
    May 11, 2020
    Posts:
    402
    im currently following codemonkeys A* pathfinding tutorial and im running into this error that im not sure how to resolve, iv looked it up on forums and on stack overflow, but it seems that every answer is a little bit different.
    this is the code that's causing the error
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Grid
    7. {
    8.     private int width;
    9.     private int height;
    10.     private float cellSize;
    11.     private Vector3 originPosition;
    12.  
    13.     private int[,] gridArray;
    14.     private TextMesh[,] debugTextArray;
    15.     public Grid(int width, int height, float cellSize, Vector3 originPosition)
    16.     {
    17.         this.width = width;
    18.         this.height = height;
    19.         this.cellSize = cellSize;
    20.         this.originPosition = originPosition;
    21.  
    22.         gridArray = new int[width, height];
    23.         debugTextArray = new TextMesh[width, height];
    24.         for (int x = 0; x < gridArray.GetLength(0); x++)
    25.         {
    26.             for (int y = 0; y < gridArray.GetLength(1); y++)
    27.             {
    28.                debugTextArray[x,y] = CreateWorldText(gridArray[x, y].ToString(), null, GetWorldPosition(x, y) + new Vector3(cellSize, cellSize) * 0.5f, TextAnchor.MiddleCenter, TextAlignment.Center, 20, Color.white, 1);
    29.                 Debug.DrawLine(GetWorldPosition(x,y), GetWorldPosition(x,y + 1), Color.white, Mathf.Infinity);
    30.                 Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, Mathf.Infinity);
    31.  
    32.             }
    33.         }
    34.  
    35.         Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, Mathf.Infinity);
    36.         Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, Mathf.Infinity);
    37.     }
    38.  
    39.     public void SetValue(int x, int y, int value)
    40.     {
    41.         if(x >= 0 && y >= 0 && x < width && y < height)
    42.         {
    43.         gridArray[x, y] = value;
    44.             debugTextArray[x, y].text = value.ToString();
    45.         }
    46.     }
    47.  
    48.     public void SetValue(Vector3 worldPos, int value)
    49.     {
    50.         int x, y;
    51.         GetCellPosition(worldPos, out x, out y);
    52.         SetValue(x, y, value);
    53.     }
    54.  
    55.     public int GetValue(int x, int y)
    56.     {
    57.         if (x >= 0 && y >= 0 && x < width && y < height)
    58.         {
    59.             return gridArray[x, y];
    60.         }
    61.         else
    62.         {
    63.             Debug.LogWarning("Invalid Pos");
    64.             return -1;
    65.         }
    66.     }
    67.     public int GetValue(Vector3 worldPos)
    68.     {
    69.         int x, y;
    70.         GetCellPosition(worldPos, out x, out y);
    71.        return(GetValue(x, y));
    72.     }
    73.     public Vector3 GetWorldPosition(int x, int y)
    74.     {
    75.         return new Vector3(x, y) * cellSize + originPosition;
    76.     }
    77.     public void GetCellPosition(Vector3 worldPosition, out int x, out int y)
    78.     {
    79.         x = Mathf.FloorToInt((worldPosition - originPosition).x / cellSize);
    80.         y = Mathf.FloorToInt((worldPosition - originPosition).y / cellSize);
    81.  
    82.     }
    83.     #region WorldText
    84.     public static TextMesh CreateWorldText(string text, Transform parent, Vector3 localPosition, TextAnchor textAnchor, TextAlignment textAlignment, int fontSize, Color textColor, int sortingOrder)
    85.     {
    86.         GameObject gameObject = new GameObject("World_Text", typeof(TextMesh));
    87.         Transform transform = gameObject.transform;
    88.         transform.SetParent(parent, false);
    89.         transform.localPosition = localPosition;
    90.         TextMesh textMesh = gameObject.GetComponent<TextMesh>();
    91.         textMesh.anchor = textAnchor;
    92.         textMesh.alignment = textAlignment;
    93.         textMesh.text = text;
    94.         textMesh.fontSize = fontSize;
    95.         textMesh.color = textColor;
    96.         textMesh.GetComponent<MeshRenderer>().sortingOrder = sortingOrder;
    97.         return textMesh;
    98.     }
    99.     #endregion
    100. }
    101.  
    Code (csharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class PathNode
    8.  
    9. {
    10.  
    11.     private Grid<PathNode> grid;
    12.     private int x;
    13.     private int y;
    14.  
    15.     public int gCost;
    16.     public int hCost;
    17.     public int fCost;
    18.  
    19.     public PathNode cameFromNode;
    20.  
    21.  
    22.     public PathNode(Grid<PathNode> grid, int x, int y)
    23.     {
    24.         this.grid = grid;
    25.         this.x = x;
    26.         this.y = y;
    27.     }
    28.     public override string ToString()
    29.     {
    30.         return x + "," + y;
    31.     }
    32. }
    33.  
    Code (csharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class Pathfinder
    8. {
    9.     private Grid<PathNode> grid;
    10.  
    11.     public Pathfinder(int width, int height)
    12.     {
    13.         grid = new Grid<PathNode>(width, height, 10f, Vector3.zero, (Grid<PathNode> grid, int x, int y) => new PathNode(grid, x, y)));
    14.     }
    15.  
    16. }
    17.  
    Sorry for the code dump,
    Normally i would be able to logic out the error, but this is completly foreign code to me, i understood the grid script well enough that was standard, but the syntax for the pathfinder and pathnode scripts is completly new, i was planning to finish the script following his tutorial to a T and then go back and dive into the code, dissecting it and whatnot, but now iv run into this error. since all the forums i read on this specific error seem to be giving different solutions based on circumstance. i dont even really know where to start
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,320
    You're trying to make a Grid<PathNode> ( a generic instance) but the class itself is not generic.

    Look up how to make a generic, eg, a Grid<T>() type of class.

    I can never remember the syntax but it has to have the <T> stuff called out in the Grid class and it's all in the MSFT docs.
     
    CodeRonnie likes this.
  3. puddleglum

    puddleglum

    Joined:
    May 11, 2020
    Posts:
    402
    thankyou ill check it out