Can't render GameObject

Hi,

I am trying to define and render a gameobject entirely in C# script (i.e. without an existing prefab).

The relevant part of my code is as follows:

	HexTile = new GameObject("Single Hex Tile");

	MeshFilter HexMeshFilter = HexTile.AddComponent<MeshFilter>();
	MeshRenderer HexMeshRenderer = HexTile.AddComponent<MeshRenderer>();
	
	Mesh HexMesh = new Mesh();
	HexMesh.vertices = HexVertices;
	HexMesh.triangles = HexTriangles;
	HexMesh.uv = HexUV;

	HexMesh.RecalculateNormals();
	
	HexMeshFilter.mesh = HexMesh;
	HexMeshRenderer.material.mainTexture = HexTexture;

	Instantiate(HexTile);

The rest of the code just defines the vertices, triangles, and uv. The code is attached to a blank gameobject. However, running it just leaves me with a blank screen.

I’m still very uncertain on the whole process of attaching a render and filter to my object and instantiating it… so was hoping that someone would be able to point out the (probably obvious) thing I am doing wrong?

Thank in advance!

When you use new GameObject() you have already instantiated a new empty gameObject, using Instantiate(gameObject) should make a second copy of this gameObject as it currently is so you should remove Instantiate(HexTile). It would also be a good idea to assign a new material to your renderer. However neither of these should make your gameObject invisible so I think the problem might be in another part of your code. Here is some code that works, try using it as a starting off point and see why your code does not create a visible object.

GameObject gameObject=new GameObject("Triangle");

MeshFilter filter=gameObject.AddComponent<MeshFilter>();
Mesh mesh=new Mesh();
mesh.vertices=new Vector3[]{
    new Vector3(0, 0),
    new Vector3(0, 1),
    new Vector3(1, 0),
};
mesh.triangles=new int[] { 
    0, 1, 2, 
};
mesh.uv=new Vector2[mesh.vertices.Length];
mesh.RecalculateNormals();
mesh.Optimize();
filter.mesh=mesh;

MeshRenderer renderer=gameObject.AddComponent<MeshRenderer>();
renderer.material=new Material(Shader.Find("Diffuse"));
renderer.material.color=Color.green;

Instead of coding this, you can just add a mesh renderer to the GameObject.

I can’t see any differences between your code and mine. This is certainly frustrating. Could it be the way I am calling the function? My full code is as below (you can ignore the second region):

using UnityEngine;
using System.Collections;

public class HexManager : MonoBehaviour {

	#region HexInfo
	
	public float HexUnitLength;
	
	[HideInInspector] public Vector3[] HexVertices;
	[HideInInspector] public Vector2[] HexUV;
	[HideInInspector] public int[] HexTriangles;
	
	public Texture HexTexture;

	[HideInInspector] public GameObject HexTile;
	
	void HexMeshSetup() {
		
		float floorLevel = 0;
		float HexSizeMult = HexUnitLength / Mathf.Sqrt(3);

		HexVertices = new Vector3[] {
			/*0*/new Vector3((HexSizeMult * Mathf.Cos((float)(2*Mathf.PI*(3+0.5)/6))), floorLevel, (HexSizeMult * Mathf.Sin((float)(2*Mathf.PI*(3+0.5)/6)))),
			/*1*/new Vector3((HexSizeMult * Mathf.Cos((float)(2*Mathf.PI*(2+0.5)/6))), floorLevel, (HexSizeMult * Mathf.Sin((float)(2*Mathf.PI*(2+0.5)/6)))),
			/*2*/new Vector3((HexSizeMult * Mathf.Cos((float)(2*Mathf.PI*(1+0.5)/6))), floorLevel, (HexSizeMult * Mathf.Sin((float)(2*Mathf.PI*(1+0.5)/6)))),
			/*3*/new Vector3((HexSizeMult * Mathf.Cos((float)(2*Mathf.PI*(0+0.5)/6))), floorLevel, (HexSizeMult * Mathf.Sin((float)(2*Mathf.PI*(0+0.5)/6)))),
			/*4*/new Vector3((HexSizeMult * Mathf.Cos((float)(2*Mathf.PI*(5+0.5)/6))), floorLevel, (HexSizeMult * Mathf.Sin((float)(2*Mathf.PI*(5+0.5)/6)))),
			/*5*/new Vector3((HexSizeMult * Mathf.Cos((float)(2*Mathf.PI*(4+0.5)/6))), floorLevel, (HexSizeMult * Mathf.Sin((float)(2*Mathf.PI*(4+0.5)/6))))
		};
		
		HexTriangles = new int[] {
			1,5,0,
			1,4,5,
			1,2,4,
			2,3,4
		};
		
		HexUV = new Vector2[] {
			new Vector2(0,0.25f),
			new Vector2(0,0.75f),
			new Vector2(0.5f,1),
			new Vector2(1,0.75f),
			new Vector2(1,0.25f),
			new Vector2(0.5f,0),
		};

		HexTile = new GameObject("Single Hex Tile");

		HexTile.transform.position = Vector3.zero;
		HexTile.transform.rotation = Quaternion.identity;
		HexTile.transform.localScale = Vector3.one;

		MeshFilter HexMeshFilter = HexTile.AddComponent<MeshFilter>();
		MeshRenderer HexMeshRenderer = HexTile.AddComponent<MeshRenderer>();
		
		Mesh HexMesh = new Mesh();
		HexMesh.vertices = HexVertices;
		HexMesh.triangles = HexTriangles;
		HexMesh.uv = HexUV;

		HexMesh.RecalculateNormals();
		
		HexMeshFilter.mesh = HexMesh;
		HexMeshRenderer.material.mainTexture = HexTexture;

	}
	
	#endregion

	#region GridGeneration

	public int GridWidth;
	public int GridHeight;

	public Vector3 C aalcInitPos() {
	
	// Position of the first tile placed will be the upper-left corner of the grid.

		Vector3 InitPos;
		InitPos = new Vector3(-HexUnitLength * GridWidth / 2f + HexUnitLength / 2,
		                      0,
		                      HexUnitLength * GridHeight / 2f - HexUnitLength / 2);
		
		return InitPos;

	}

	public Vector3 CalcWorldCoord(Vector2 GridPos) {
	
		Vector3 InitPos = CalcInitPos();

		float RowOffset = 0;
		if (GridPos.y % 2 != 0)
			RowOffset = HexUnitLength / 2;
		
		float x =  InitPos.x + RowOffset + GridPos.x * HexUnitLength;
		float z = InitPos.z - GridPos.y * HexUnitLength * 0.75f;
		return new Vector3(x, 0, z);

	}

	void CreateGrid() {
	
	//HexGrid will be a game object which is the parent of all the individual hex tiles
	
		GameObject HexGrid = new GameObject("HexGrid");
		
		for (float y = 0; y < GridHeight; y++) {
		
			for (float x = 0; x < GridWidth; x++) {
			
				Vector2 GridPos = new Vector2(x, y);
				GameObject HexClone = (GameObject)Instantiate(HexTile);
				HexClone.transform.position = CalcWorldCoord(GridPos);
				HexClone.transform.parent = HexGrid.transform;
			}
		}
	}

	#endregion

	#region Run

	void start() {

		HexMeshSetup();
		CreateGrid();

	}

	#endregion

}