creating a mesh from script

i have no idea why this code isn’t working… its almost exactly what is placed in the scripting reference

using UnityEngine;
using System.Collections;

public class Window2 : MonoBehaviour {
	
	
  	public Vector3[] Vertices;
    public Vector2[] UV;
    public int[] Triangles;
	
	void MeshSetup(){
	 Vector3[] Vertices = new Vector3[] {new Vector3(-1,0,1),new Vector3(1,0,1),new Vector3(1,0,-1),new Vector3(-1,0,-1)};
     Vector2[] UV = new Vector2[] {new Vector2(0,256),new Vector2(256,256),new Vector2(256,0),new Vector2(0,0)};
     int[] Triangles = new int[] {0,1,2,0,2,3};
		
	}
	
	
    void Start() {
		MeshSetup();
        Mesh stuff = new Mesh();
		gameObject.AddComponent<MeshFilter>().mesh = stuff;
		stuff.vertices = Vertices;
		stuff.triangles = Triangles;
        stuff.uv = UV;
		
	}
}

so thats what i have, judging by everything i have read, that should work. right now i get 0 verts , 0 tris on the mesh that is added…

Dude you’re made a trivial mistake

(purely at the language level, unrelated to mesh technology)

In lines 12 through 15 you’re delcaring NEW local variables inside the routine MeshSetup !

wrong …

Vector3[] Vertices = ...
Vector2[] UV = ...
int[] Triangles = ...

correct…

Vertices = ...
UV = ...
Triangles = ...

Hope it helps!

PS1: a good trick is, ALSO just add a mesh collider with the same mesh, very simply so you can seee it in the editor, evene if you have not yet added an actual texture to the visual mesh. So simply add the line

gameObject.AddComponent<MeshCollider>().mesh = stuff;

as the final line in Start(). TBC, note that of course you would not add collider mesh, at runtime in a game, only at startup time. Info here: adding mesh collider in run time slow? - Questions & Answers - Unity Discussions

PS2: you will eventually need some normals. Creating normals is an art form, it is not something that can be done by the same software every time. Unity gives you for free a “rough and ready” normals routine which can be handy during development. it is called RecalculateNormals(). Understand that later you will have to write your own subtle normals-making software. Here is a discussion about that issue Do we need to call RecalculateNormals() if normal vectors are assigned? - Questions & Answers - Unity Discussions

You are re-declaring the type of Vertices, UV, Triangle variables inside of the MeshSetup() method. As you have it now, they are treated as local variables, and their values are not assigned to the publicly declared variables of the same name, so, whatever is assigned to those values when the method runs are immediately forgoten when the method finishes.

This should work:

using UnityEngine;
using System.Collections;

public class Window2 : MonoBehaviour {


    public Vector3[] Vertices;
    public Vector2[] UV;
    public int[] Triangles;

    void MeshSetup(){
     Vertices = new Vector3[] {new Vector3(-1,0,1),new Vector3(1,0,1),new Vector3(1,0,-1),new Vector3(-1,0,-1)};
     UV = new Vector2[] {new Vector2(0,256),new Vector2(256,256),new Vector2(256,0),new Vector2(0,0)};
     Triangles = new int[] {0,1,2,0,2,3};

    }


    void Start() {
       MeshSetup();
        Mesh stuff = new Mesh();
       gameObject.AddComponent<MeshFilter>().mesh = stuff;
       stuff.vertices = Vertices;
       stuff.triangles = Triangles;
        stuff.uv = UV;

    }
}