Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2002
    Location
    20631 Autumn Terrace Ln Katy,TX77450
    Posts
    22

    Exclamation Null Reference Exception Adding Vertices to Mesh.

    I keep getting the error message "Null Reference Exception" on the line where I add vertices to a mesh. Here is the code.


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

  2. #2
    Join Date
    Aug 2010
    Location
    Vector3.Zero
    Posts
    146
    ok, are you applying any mesh to to the mesh property inside the mesh filter tab? because, when u say

    Mesh mesh = newMesh.sharedMesh;

    if u didnt assign a mesh in the editor window after you add this script, this would def generate a nullRefException cuz there is no mesh for the Mesh component to hold inside the variable. If you just use mesh instead of sharedMesh, when you run the program it will automatically assign whatever gameObject that the script is on as a mesh object. If you use sharedMesh you have to manually assign a mesh in the editor window. Hope this helped
    Noob stuff I've been playing with

    Multiplayer RPG - http://www.youtube.com/user/Stridervan?feature=mhee
    Advanced Minimap Tutorial - http://www.youtube.com/playlist?list...ature=view_all
    Website - http://projectzoso.weebly.com/

  3. #3
    Join Date
    Mar 2002
    Location
    20631 Autumn Terrace Ln Katy,TX77450
    Posts
    22
    OK, I assigned newmesh as a public Mesh in the top variables section and selected a Plane primitive to the Mesh field in the Meshfilter component in the editor window. I tried several primitives and they all work but I thought you could just use an empty GO to make meshes.

    Also, it seems you have to destroy the mesh in code in order for it to dissapear. Lastly i wanna ask you if you have any links or advices on the best practices to keeping the vertex order straight. I notice some of my triangles are backfacing or not touch the correct corners.
    Thanks so much for pointing out the mesh thing, I noticed at runtime that is was not picking up the "newmesh" variable...

  4. #4
    Join Date
    Aug 2010
    Location
    Vector3.Zero
    Posts
    146
    Quote Originally Posted by SPACEY View Post
    ...I thought you could just use an empty GO to make meshes...
    Yes yes, You CAN create mesh from an empty GO. I thought you were going to modify the mesh since you used sharedMesh from the mesh filter before even creating the mesh.

    You have to create the mesh first then you need toassign it to the meshfilter of w/e component you were trying to add like this

    Code:
    Mesh mesh = new Mesh();
    this.getComponent<MeshFiler>().mesh = mesh;
    mesh.vertices = vertexVector; // this is where you stored the vertex info
    mesh.triangles = trisVector; // vector where you store tris info
    mesh.uv = uvVector; // uv vector
    mesh.uv2 = un2Vector; // and so on
    // you can even recalculate normals and other stuff
    sharedMesh needs to be shared among other GOs. And DONT ever use SharedMesh to write data. use it only for rendering purposes. So for this purpose, unity throws an exception when using sharedMesh when its not assigned. You never wanna ever change the data of a sharedMesh, since it will be shared by other gameObjects, when you change the sharedMesh of an object, the other GOs thats linked with the same sharedMesh will be changed(so its not recommeded).

    Yes, thats an important part, you DO have to destroy the mesh. If you create them in run-time, you may want to delete the filter, collider, and mesh in the reverse order you created them(to save some memory, since GC sometimes doesnt clear them automatically)

    The backface problem is caused because the direction you draw the vertices matter, you have to use Clockwise direction for front facing triangle. If you use CCW it will be backfaced.
    Noob stuff I've been playing with

    Multiplayer RPG - http://www.youtube.com/user/Stridervan?feature=mhee
    Advanced Minimap Tutorial - http://www.youtube.com/playlist?list...ature=view_all
    Website - http://projectzoso.weebly.com/

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •