Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. The 2023.1 beta is now available for testing. To find out what's new, have a look at our 2023.1 feature highlights.
    Dismiss Notice

Scripting Setting mesh.uv throws Null Reference Error

Discussion in '5.4 Beta' started by karl_, Dec 29, 2015.

  1. karl_

    karl_

    Joined:
    Mar 4, 2010
    Posts:
    464
    I don't see any warnings about uv or uv2 being deprecated, so I'm assuming this is a bug.

    Example code:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [RequireComponent(typeof(MeshFilter))]
    6. [RequireComponent(typeof(MeshRenderer))]
    7. public class DoPlane : MonoBehaviour
    8. {
    9.     void Start ()
    10.     {
    11.         Mesh m = new Mesh();
    12.  
    13.         m.vertices = new Vector3[]
    14.         {
    15.             Vector2.zero,
    16.             Vector2.right,
    17.             Vector2.up,
    18.             Vector2.one
    19.         };
    20.  
    21.         m.normals = new Vector3[]
    22.         {
    23.             Vector3.forward,
    24.             Vector3.forward,
    25.             Vector3.forward,
    26.             Vector3.forward
    27.         };
    28.  
    29.         m.triangles = new int[] { 0, 1, 2, 1, 3, 2 };
    30.  
    31.         // NullReferenceException: Object reference not set to an instance of an object
    32.         // UnityEngine.Mesh.SetArrayForChannel[Vector2] (InternalShaderChannel channel, UnityEngine.Vector2[] values) (at C:/buildslave/unity/build/Runtime/Export/Mesh.cs:82)
    33.         // UnityEngine.Mesh.set_uv2 (UnityEngine.Vector2[] value) (at C:/buildslave/unity/build/Runtime/Export/Mesh.cs:117)
    34.         // DoPlane.Start () (at Assets/DoPlane.cs:32)
    35.         //
    36.         // at this line
    37.         m.uv = new Vector2[]
    38.         {
    39.             Vector2.zero,
    40.             Vector2.right,
    41.             Vector2.up,
    42.             Vector2.one
    43.         };
    44.  
    45.         m.uv2 = null;
    46.  
    47.         GetComponent<MeshFilter>().sharedMesh = m;
    48.     }
    49. }
    50.  
     
  2. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,542
    hm, we redid under-the-hood uvs handling and it seems that felt through cracks. Though i totally must ask why do you assign null at all? ;-)
     
  3. karl_

    karl_

    Joined:
    Mar 4, 2010
    Posts:
    464
    In this example it doesn't make sense, but with the plugin I work on I regularly rebuild the positions and uv arrays which throws uv2 out of sync. As an optimization I don't clear the entire mesh unless the vertex count changes (because some of the mesh properties aren't cached, they would need to be recalculated which can be heavy). Leaving uv2 intact can look bad if lightmaps were baked to an object, so hence setting null.

    As an aside, I really like the way UVs are implemented now. Access to 3 additional vec4 channels is awesome :)
     
  4. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,542
    "Leaving uv2 intact can look bad if lightmaps were baked to an object, so hence setting null."
    oh so you refer to tweaking mesh (not just creating) - yeah kinda make sense. Just so i dont forget about it, can you please create bug report with small repro (well just scene with script is enough) and drop case number here?
     
    landon912 likes this.
  5. karl_

    karl_

    Joined:
    Mar 4, 2010
    Posts:
    464
  6. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,542
    fixed, should go into next beta (or if we manage to screw things with merging one after next)
    release notes to look for (well, it might be reworded if native speakers start whining ;)) :
    Graphics: restored possibility to set null for mesh components
     
    karl_ likes this.
  7. karl_

    karl_

    Joined:
    Mar 4, 2010
    Posts:
    464
    Awesome, thanks Alexey!