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
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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Huge GC Overhead When Accessing Tris/Verts From Mesh? 2 Answers

Accessing Mesh: Triangle and Vertex array fields directly? 0 Answers

Why does the default Unity sphere have duplicate vertices? 1 Answer

Problem Creating a 2D Mesh 1 Answer

Procedural Mesh Generation - Split Arrays into sections 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