it previously worked but i had to drag a material into the inspector to set it. i thought it would be neat to just set it in code rather than dragging and dropping. after some research i found a way, but it now will only spawn the cube when i drag the script into the gameobject on play.
meshbuilder.cs
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshFilter))]
public class meshbuilder : MonoBehaviour
{
//GameObject Cube = new GameObject();
public Material Red = Resources.Load("Red",typeof(Material)) as Material;
//front face
public Vector3 vertice0 = new Vector3(-1,1,1);
public Vector3 vertice1 = new Vector3(1,1,1);
public Vector3 vertice2 = new Vector3(-1,-1,1);
public Vector3 vertice3 = new Vector3(1,-1,1);
//back face
public Vector3 vertice4 = new Vector3(1,1,-1);
public Vector3 vertice5 = new Vector3(-1,1,-1);
public Vector3 vertice6 = new Vector3(1,-1,-1);
public Vector3 vertice7 = new Vector3(-1,-1,-1);
//left face
public Vector3 vertice8 = new Vector3(-1,1,-1);
public Vector3 vertice9 = new Vector3(-1,1,1);
public Vector3 vertice10 = new Vector3(-1,-1,-1);
public Vector3 vertice11 = new Vector3(-1,-1,1);
//right face
public Vector3 vertice12 = new Vector3(1,1,1);
public Vector3 vertice13 = new Vector3(1,1,-1);
public Vector3 vertice14 = new Vector3(1,-1,1);
public Vector3 vertice15 = new Vector3(1,-1,-1);
//top face
public Vector3 vertice16 = new Vector3(-1,1,-1);
public Vector3 vertice17 = new Vector3(1,1,-1);
public Vector3 vertice18 = new Vector3(-1,1,1);
public Vector3 vertice19 = new Vector3(1,1,1);
//bottom face
public Vector3 vertice20 = new Vector3(-1,-1,1);
public Vector3 vertice21 = new Vector3(1,-1,1);
public Vector3 vertice22 = new Vector3(-1,-1,-1);
public Vector3 vertice23 = new Vector3(1,-1,-1);
// Use this for initialization
void Start ()
{
MeshFilter Filtration = GetComponent<MeshFilter>();
Mesh CubeMorph = Filtration.mesh;
GetComponent<MeshRenderer>().material = Red;
//VerticesArray
Vector3[] VertArray = new Vector3[]
{
//Front Face
vertice0,//0
vertice1,//1
vertice2,//2
vertice3,//3
//back face
vertice4,//4
vertice5,//5
vertice6,//6
vertice7,//7
//left face
vertice8,//8
vertice9,//9
vertice10,//10
vertice11,//11
//right face
vertice12,//12
vertice13,//13
vertice14,//14
vertice15,//15
//top face
vertice16,//16
vertice17,//17
vertice18,//18
vertice19,//19
//bottom face
vertice20,//20
vertice21,//21
vertice22,//22
vertice23//23
};
//triangles array clockwise points equals visablity
int[] triArray = new int[]
{
//front face
0,2,3,
3,1,0,
//back face
4,6,7,
7,5,4,
//left face
8,10,11,
11,9,8,
//right face
12,14,15,
15,13,12,
//top face
16,18,19,
19,17,16 ,
//bottom face
20,22,23,
23,21,20
};
//uvs
Vector2[] uvs = new Vector2[]
{
//front face
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0),
//back face
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0),
//left face
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0),
//right face
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0),
//top face
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0),
//bottom face
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,1),
new Vector2(1,0)
};
CubeMorph.Clear();
CubeMorph.vertices = VertArray;
CubeMorph.triangles = triArray;
CubeMorph.uv = uvs;
CubeMorph.Optimize();
CubeMorph.RecalculateNormals();
}
// Update is called once per frame
//void Update () {
//
//}
}