Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by iamthecoolguy11 · Jan 19, 2014 at 01:35 AM · c#errortexturemesh

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)

alt text

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);
                 }
                 
             }
         }
     }
 
 
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iamthecoolguy11 · Jan 19, 2014 at 01:46 AM 0
Share

Thank you :)

avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iamthecoolguy11 · Jan 19, 2014 at 01:46 AM 0
Share

also thank you :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges