This is baffling me, but I bet the answer is so simple I’d kick myself. I’m following Catlike Coding’s HexMap tutorial (part 1) and this error has cropped up:
Assets/Scripts/HexMesh.cs(23,25): error CS1061: Type UnityEngine.Mesh' does not contain a definition for
Triangulate’ and no extension method Triangulate' of type
UnityEngine.Mesh’ could be found (are you missing a using directive or an assembly reference?)
Does anyone know why it’s being such a pain? Here are my scripts, and I’ll point out where the error is coming from. On the first script, in the Start command.
using UnityEngine;
using System.Collections;
using System.Collections.Generic; //Be able to use lists
//Get a mesh filter and renderer
[RequireComponent(typeof(MeshFilter),typeof(MeshRenderer))]
public class HexMesh : MonoBehaviour {
Mesh hexMesh; //Call hexMesh
List<Vector3> vertices; //List for vertices
List<int> triangles; //List for triangles
//When scene awakens. . .
void Awake(){
GetComponent<MeshFilter> ().mesh = hexMesh = new Mesh (); //Get mesh filter component and hex mesh
hexMesh.name = "Hex Mesh"; //Get name Hex Mesh
vertices = new List<Vector3> (); //Get vertices list
triangles = new List<int> (); //Get triangles list
}
void Start(){
hexMesh.Triangulate (cells); //Tell mesh to triangulate cells. Put it in Start function to make sure it happens after hex mesh component has awoken as well.
}
public void Triangulate(HexCell[] cells){
hexMesh.Clear (); //Clear old data
vertices.Clear (); //Clear old data
triangles.Clear (); //Clear old data
//Loop through all cells, triangulating them individually
for (int i = 0; i < cells.Length; i++) {
Triangulate(cells[i]);
}
hexMesh.vertices = vertices.ToArray (); //Assign generated vertices to mesh
hexMesh.triangles = triangles.ToArray (); //Assign generated triangles to mesh
hexMesh.RecalculateNormals (); //Recalculate mesh normals
}
void Triangulate(HexCell cell){
Vector3 center = cell.transform.localPosition;
AddTriangle (center,
center + HexMetrics.corners [0],
center + HexMetrics.corners [1]
);
}
void AddTriangle (Vector3 v1, Vector3 v2, Vector3 v3){
int vertexIndex = vertices.Count;
vertices.Add (v1);
vertices.Add (v2);
vertices.Add (v3);
triangles.Add (vertexIndex);
triangles.Add (vertexIndex + 1);
triangles.Add (vertexIndex + 2);
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class HexGrid : MonoBehaviour {
public int width = 6;
public int height = 6;
public HexCell cellPrefab; //Cell prefab public variable
public Text cellLabelPrefab; //Label prefab public variable
Canvas gridCanvas; //Get gridCanvas
HexCell[] cells; //Get HexCell cells
HexMesh hexMesh; //Get HexMesh
void Awake(){
gridCanvas = GetComponentInChildren<Canvas> (); //Call and find canvas
hexMesh = GetComponentInChildren<HexMesh> (); //Allow HexGrid to retrieve HexMesh
cells = new HexCell[height * width];
for (int z = 0, i = 0; z < height; z++) {
for (int x = 0; x < width; x++){
CreateCell(x,z,i++);
}
}
}
void CreateCell(int x, int z, int i){
Vector3 position;
position.x = (x + z * 0.5f - z / 2) * (HexMetrics.innerRadius * 2f); //This gives the correct hexagon positions
position.y = 0f;
position.z = z * (HexMetrics.outerRadius * 1.5f);
//Make the cells 10 by 10 units.
HexCell cell = cells [i] = Instantiate<HexCell> (cellPrefab);
cell.transform.SetParent (transform, false);
cell.transform.localPosition = position;
Text label = Instantiate<Text> (cellLabelPrefab);
label.rectTransform.SetParent (gridCanvas.transform, false);
label.rectTransform.anchoredPosition = new Vector2 (position.x, position.z);
label.text = x.ToString () + "\n" + z.ToString ();
//Show cell coordinates
}
//After grid has awoken. . .
// void Start(){
// hexMesh.Triangulate (cells); //Tell mesh to triangulate cells. Put it in Start function to make sure it happens after hex mesh component has awoken as well.
//
// }
//
// public void Triangulate(HexCell[] cells){
// hexMesh.Clear ();
// vertices.Clear ();
//
// }
}
using UnityEngine;
using System.Collections;
public static class HexMetrics {
public const float outerRadius = 10f; //Edge length of radius
public const float innerRadius = outerRadius * 0.866025404f; //Inner radius, distance from centre to each of the edges
public static Vector3[] corners = {
new Vector3(0f, 0f, outerRadius),
new Vector3(innerRadius, 0f, 0.5f * outerRadius),
new Vector3(innerRadius, 0f, -0.5f * outerRadius),
new Vector3(0f, 0f, -outerRadius),
new Vector3(-innerRadius, 0f, -0.5f * outerRadius),
new Vector3(-innerRadius, 0f, 0.5f * outerRadius)
};
//Defining positions of six corners relative to cell's center. Start by putting corner at top, then adding rest clockwise order. Place in XZ plane so hexagons are aligned with ground.
}