- Home /
how do i fix this error
I am following this tut and I cant figure out were this error is coming from.
  hears the tut
     http://studentgamedev.blogspot.no/2013/08/VoxelTutP2.html
 
     the error
     NullReferenceException
     UnityEngine.Mesh.Clear () (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/Graphics.cs:609)
     PolygonGenerator.UpdateMesh () (at Assets/PolygonGenerator.cs:84)
     PolygonGenerator.Start () (at Assets/PolygonGenerator.cs:33)

hears the script
 using UnityEngine;
 using System.Collections.Generic;
 
 public class PolygonGenerator : MonoBehaviour 
 {
     // This first list contains every vertex of the mesh that we are going to render
     public List<Vector3> newVertices = new List<Vector3>();
     
     // The triangles tell Unity how to build each section of the mesh joining
     // the vertices
     public List<int> newTriangles = new List<int>();
     
     // The UV list is unimportant right now but it tells Unity how the texture is
     // aligned on each polygon
     public List<Vector2> newUV = new List<Vector2>();
     
     
     // A mesh is made up of the vertices, triangles and UVs we are going to define,
     // after we make them up we'll save them as this mesh
     private Mesh mesh;
 
     private float tUnit = 0.25f;
     private Vector2 tStone = new Vector2 (0, 0);
     private Vector2 tGrass = new Vector2 (0, 1);
 
     private int squareCount;
 
     public byte[,] blocks;
 
     void Start () {
         GenTerrain();
         BuildMesh();
         UpdateMesh();
 
         mesh = GetComponent<MeshFilter> ().mesh;
         
         float x = transform.position.x;
         float y = transform.position.y;
         float z = transform.position.z;
         
         newVertices.Add( new Vector3 (x  , y  , z ));
         newVertices.Add( new Vector3 (x + 1 , y  , z ));
         newVertices.Add( new Vector3 (x + 1 , y-1 , z ));
         newVertices.Add( new Vector3 (x  , y-1 , z ));
         
         newTriangles.Add(0);
         newTriangles.Add(1);
         newTriangles.Add(3);
         newTriangles.Add(1);
         newTriangles.Add(2);
         newTriangles.Add(3);
         
         newUV.Add(new Vector2 (tUnit * tStone.x, tUnit * tStone.y + tUnit));
         newUV.Add(new Vector2 (tUnit * tStone.x + tUnit, tUnit * tStone.y + tUnit));
         newUV.Add(new Vector2 (tUnit * tStone.x + tUnit, tUnit * tStone.y));
         newUV.Add(new Vector2 (tUnit * tStone.x, tUnit * tStone.y));
 
     }
 
 
 
     void GenSquare(int x, int y, Vector2 texture){
         newVertices.Add( new Vector3 (x  , y  , 0 ));
         newVertices.Add( new Vector3 (x + 1 , y  , 0 ));
         newVertices.Add( new Vector3 (x + 1 , y-1 , 0 ));
         newVertices.Add( new Vector3 (x  , y-1 , 0 ));
         
         newTriangles.Add(squareCount*4);
         newTriangles.Add((squareCount*4)+1);
         newTriangles.Add((squareCount*4)+3);
         newTriangles.Add((squareCount*4)+1);
         newTriangles.Add((squareCount*4)+2);
         newTriangles.Add((squareCount*4)+3);
         
         newUV.Add(new Vector2 (tUnit * texture.x, tUnit * texture.y + tUnit));
         newUV.Add(new Vector2 (tUnit*texture.x+tUnit, tUnit*texture.y+tUnit));
         newUV.Add(new Vector2 (tUnit * texture.x + tUnit, tUnit * texture.y));
         newUV.Add(new Vector2 (tUnit * texture.x, tUnit * texture.y));
         
         squareCount++;
     }
 
     void UpdateMesh () {
         mesh.Clear ();
         mesh.vertices = newVertices.ToArray();
         mesh.triangles = newTriangles.ToArray();
         mesh.uv = newUV.ToArray();
         mesh.Optimize ();
         mesh.RecalculateNormals ();
         
         squareCount=0;
         newVertices.Clear();
         newTriangles.Clear();
         newUV.Clear();
         
     }
 
     void GenTerrain(){
         blocks=new byte[10,10];
         
         for(int px=0;px<blocks.GetLength(0);px++){
             for(int py=0;py<blocks.GetLength(1);py++){
                 if(py==5){
                     blocks[px,py]=2;
                 } else if(py<5){
                     blocks[px,py]=1;
                 }
             }
         }
     }
 
     void BuildMesh(){
         for(int px=0;px<blocks.GetLength(0);px++){
             for(int py=0;py<blocks.GetLength(1);py++){
                 
                 if(blocks[px,py]==1){
                     GenSquare(px,py,tStone);
                 } else if(blocks[px,py]==2){
                     GenSquare(px,py,tGrass);
                 }
                 
             }
         }
     }
 
 
 }
Answer by getyour411 · Jan 19, 2014 at 01:41 AM
Try moving this
  mesh = GetComponent<MeshFilter> ().mesh;
at first in Start(); seems like as you have it now, mesh is nothing when mesh.Clear is called.
Answer by KellyThomas · Jan 19, 2014 at 01:42 AM
Your Start() include the following:
 void Start () {
    GenTerrain();
    BuildMesh();
    UpdateMesh();
    mesh = GetComponent<MeshFilter> ().mesh;
    // ...
On line 84 (in UpdateMesh()) you access the variable mesh before it has been initialised.
I would move the "`mesh = GetComponent ().mesh;`" line to the first line of your Start() method. 
Your answer
 
 
             Follow this Question
Related Questions
Mesh texture not properly rendered 0 Answers
Mesh Creator (editor script) At Runtime 1 Answer
Multiple Cars not working 1 Answer
Colour cubes after CombineMesh() 0 Answers
How to texture an irregular (hexagon) shape (mesh) ? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                