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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
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);
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 Kochbd · Jun 22, 2014 at 05:02 PM 0
Share

thank you,that worked!

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

2 People are following this question.

avatar image avatar image

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


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