Setting mesh.uv throws Null Reference Error

I don't see any warnings about uv or uv2 being deprecated, so I'm assuming this is a bug.

Example code:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class DoPlane : MonoBehaviour
{
    void Start () 
    {
        Mesh m = new Mesh();

        m.vertices = new Vector3[] 
        {
            Vector2.zero,
            Vector2.right,
            Vector2.up,
            Vector2.one
        };

        m.normals = new Vector3[]
        {
            Vector3.forward,
            Vector3.forward,
            Vector3.forward,
            Vector3.forward
        };

        m.triangles = new int[] { 0, 1, 2, 1, 3, 2 };

        // NullReferenceException: Object reference not set to an instance of an object
        // UnityEngine.Mesh.SetArrayForChannel[Vector2] (InternalShaderChannel channel, UnityEngine.Vector2[] values) (at C:/buildslave/unity/build/Runtime/Export/Mesh.cs:82)
        // UnityEngine.Mesh.set_uv2 (UnityEngine.Vector2[] value) (at C:/buildslave/unity/build/Runtime/Export/Mesh.cs:117)
        // DoPlane.Start () (at Assets/DoPlane.cs:32)
        //
        // at this line
        m.uv = new Vector2[]
        {
            Vector2.zero,
            Vector2.right,
            Vector2.up,
            Vector2.one
        };

        m.uv2 = null;

        GetComponent<MeshFilter>().sharedMesh = m;
    }
}

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? ;-)

[quote=“Alexey”, post:2, topic: 610732]
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? :wink:
[/quote]

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

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

1 Like

https://fogbugz.unity3d.com/default.asp?757963_0tmnjv1kk7s09g13

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

1 Like

Awesome, thanks Alexey!