• 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
0
Question by SPACEY · Dec 07, 2011 at 10:58 PM · meshverticestriangles

Null Reference Exception Assigning Vertices to Mesh

I am getting a Null Reference Exception during runtime on the line where I assign the vertices to the mesh here is the code...

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 
 [RequireComponent (typeof (MeshCollider))]
 [RequireComponent (typeof (MeshFilter))]
 [RequireComponent (typeof (MeshRenderer))]
 public class Geometric: MonoBehaviour
 {
     public float blockheight = 1.0f;    
     public Material blockmaterial;
     public PhysicMaterial physical;
     public MeshRenderer meshrenderer;        
     public GameObject newbox;
     public int t = 0;    
     void Update()    
     {    
         t++;    
         if(t == 5)
             CreateBox();
     }
 
     public void CreateBox()
     {
         Vector3 p1 = new Vector3(0,0,0);
         Vector3 p2 = new Vector3(4,0,0);
         MeshFilter meshFilter = GetComponent<MeshFilter>();        
         Mesh newmesh = meshFilter.sharedMesh;        
         //newmesh.Clear();    
 
         //FIND SOME VERTICES        
         //wherever p is located the z plane will be placed realtive to the coordinate        
         Vector3 topLeftFront = p1;
         Vector3 topRightFront = p2;
         Vector3 topLeftBack = p1;
         Vector3 topRightBack = p2;
 
         topRightFront.z =.5f;
         topLeftFront.z = .5f;
         topLeftBack.z = -.5f;
         topRightBack.z = -.5f;    
 
         //copy some coordinates to alter from previois vertices assignments        
         Vector3 bottomLeftFront = topLeftFront;        
         Vector3 bottomRightFront = topRightFront;
         Vector3 bottomLeftBack = bottomLeftFront;
         Vector3 bottomRightBack = bottomRightFront;
 
         bottomLeftBack.y = bottomLeftFront.y = bottomRightBack.y = bottomRightFront.y = -1*blockheight;        
 
         //assign the vertex positions to the mesh vertices array        
         newmesh.vertices = new Vector3[]{topLeftFront,topRightFront,topLeftBack,topRightBack,bottomLeftFront,
                            bottomRightFront,bottomLeftBack,bottomRightBack};
 
         //assign uvs based on vertices in array?        
         Vector2[] uvs = new Vector2[newmesh.vertices.Length];    
         for(int i=0; i < newmesh.vertices.Length; i++)
         {
             Debug.Log(newmesh.vertices[i]);    
             uvs[i] = new Vector2(newmesh.vertices[i].x, newmesh.vertices[i].z);    
         }
         newmesh.uv = uvs;
 
         //group sets of 3 vertices into triagles endpoints        
         newmesh.triangles = new int[]{0,1,5,1,5,6,0,2,3,2,3,1,2,0,4,0,4,6,2,3,7,3,7,4,3,1,5,1,5,7,4,6,7,6,7,5};
 
         //box the mesh                
         newmesh.RecalculateNormals();
         newmesh.RecalculateBounds();
         newmesh.Optimize();
     }            
 }
Comment
Add comment · Show 2
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 luizgpa · Dec 07, 2011 at 11:28 PM 1
Share

So 'newmesh' is null, right? And that means 'meshFilter.shared$$anonymous$$esh' is also null.

avatar image Bunny83 · Dec 08, 2011 at 12:01 AM 0
Share

... and that means there is no $$anonymous$$esh object so you have to create one.

 newmesh = new $$anonymous$$esh();
 
 //...
 
 meshFilter.shared$$anonymous$$esh = newmesh;

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Best Way to Link Vertices into Mesh 0 Answers

Voxel mesh generation not working. 1 Answer

Best way to optimize mesh updating through code? 1 Answer

Mesh triangles don't match wireframe view? 0 Answers

Realtime mesh triangles do not appear depending on camera angle 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges