- Home /
Question by
Kochbd · Jun 22, 2014 at 08:42 AM ·
c#nullreferenceexceptionobject reference
Object reference not set to an instance of an object
I am translating a script that generates a hexagonal mesh into C# from java and I ran into this issue with the uvs, it works fine except when I try to assign uvs and I get the above error on the line reading "uvVerts[i] = v * uvRadius;" at line 69. Any help would be appreciated.
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshCollider))]
public class HexGrid : MonoBehaviour {
public int columns = 100;
public int rows = 50;
public float tileSize = 1.0f;
public Material material;
private Color[] colors;
private Mesh mesh; // = new Mesh();
private Vector2[] uvVerts; //= new Vector2[6];
private float uvRadius;
private Vector2 uvDefault;
public int uvRows = 2; // Number of rows in the texture
public int uvCols = 2; // Number of cols in the texture
// Use this for initialization
void Start () {
mesh = new Mesh();
Vector2[] uvVerts = new Vector2[6];
float uvRadius = 1.0f / uvCols / 2.0f;
Vector2 uvDefault = new Vector2(uvRadius, uvRadius);
renderer.material = material;
BuildMesh();
// Set all the cells to different colors
for (int i = 0; i < rows; i++) {
for (var j = 0; j < columns; j++) {
SetUV(i,j,i%2,j%2);
}
}
}
public void BuildMesh() {
//mesh.normals = normals;
// Assign our mesh to our filter/renderer/collider
MeshFilter mesh_filter = GetComponent<MeshFilter>();
MeshRenderer mesh_renderer = GetComponent<MeshRenderer>();
MeshCollider mesh_collider = GetComponent<MeshCollider>();
mesh = new Mesh();
mesh_filter.mesh = mesh;
float vertSpacing = 2.0f * Mathf.Cos(30.0f * Mathf.Deg2Rad) * tileSize;
float horzSpacing = tileSize + Mathf.Sin(30.0f * Mathf.Deg2Rad) * tileSize;
Vector3 currPos = new Vector3(-columns / 2.0f * horzSpacing, 0.0f, rows / 2.0f * vertSpacing);
//layout vertices of a single hex
Vector3[] hexVerts = new Vector3[6];
hexVerts[0] = Vector3.zero;
Vector3 v = Vector3.right;
for (int i = 0; i < 6; i++) {
hexVerts[i] = v * tileSize;
uvVerts[i] = v * uvRadius; //THIS LINE
v = Quaternion.AngleAxis(60.0f, -Vector3.down) * v;
}
//create the vertices
Vector3[] vertices = new Vector3[rows * columns * 6];
int currVert = 0;
for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
for (int k = 0; k < hexVerts.Length; k++) {
vertices[currVert++] = hexVerts[k] + currPos;
}
currPos.z-= vertSpacing;
}
currPos.x += horzSpacing;
currPos.z = rows / 2.0f * vertSpacing;
if (i % 2 == 0)
currPos.z -= vertSpacing / 2.0f;
}
mesh.vertices = vertices;
Debug.Log("vertices done");
//create the triangles
int current = 0;
int[] triangles = new int[rows * columns * 4 * 3];
for (int i = 0; i < columns * rows * 6; i += 6) {
triangles[current++] = i+0;
triangles[current++] = i+1;
triangles[current++] = i+5;
triangles[current++] = i+5;
triangles[current++] = i+1;
triangles[current++] = i+4;
triangles[current++] = i+4;
triangles[current++] = i+1;
triangles[current++] = i+2;
triangles[current++] = i+2;
triangles[current++] = i+3;
triangles[current++] = i+4;
}
mesh.triangles = triangles;
// Setup a based set of UV coordinates
Vector2[] uvs = new Vector2[vertices.Length];
for (int i = 0; i < vertices.Length; i += 6) {
for (int j = 0; j < 6; j++) {
uvs[i+j] = uvVerts[j] + uvDefault;
}
}
mesh.uv = uvs;
mesh.colors = colors;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
mesh_filter.mesh = mesh;
mesh_collider.sharedMesh = mesh;
Debug.Log ("Done Mesh!");
}
public void SetUV(int row, int col, int atlasRow, int atlasCol) {
if (row < 0 || row >= rows) return;
if (col < 0 || col >= columns) return;
if (atlasRow < 0 || atlasRow >= uvRows) return;
if (atlasCol < 0 || atlasCol >= uvCols) return;
Vector2[] uvs = mesh.uv;
int Base = col * rows * 6 + row * 6;
Vector2 offset = new Vector2(atlasCol * uvRadius * 2.0f + uvRadius, atlasRow * uvRadius * 2.0f + uvRadius);
for (int i = 0; i < 6; i++) {
uvs[Base + i] = uvVerts[i] + offset;
}
mesh.uv = uvs;
}
}
Comment
Best Answer
Answer by tanoshimi · Jun 22, 2014 at 08:47 AM
In Start(), instead of initialising uvVerts, you're declaring a new local variable uvVerts, so that when you come to try to use it in line 69, it's still unitialised. Change lines 30-32 from:
Vector2[] uvVerts = new Vector2[6];
float uvRadius = 1.0f / uvCols / 2.0f;
Vector2 uvDefault = new Vector2(uvRadius, uvRadius);
to
uvVerts = new Vector2[6];
uvRadius = 1.0f / uvCols / 2.0f;
uvDefault = new Vector2(uvRadius, uvRadius);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
NullReferenceException problem 2 Answers
NullReferenceException 1 Answer
Distribute terrain in zones 3 Answers
Picking Weapons Up with Raycast 1 Answer