- Home /
Instantiating Mesh in edit mode? C#
Good Afternoon, I have been trying to build a mesh in edit mode.
Using MeshFilter.mesh in editor gives error
Instantiating mesh due to calling MeshFilter.mesh during edit mode. This will leak meshes
It recommends using MeshFilter.SharedMesh, I cannot use this as i want each mesh to be a separate object that can be manipulated.
Thanks Again for all your help.
Current Code
using UnityEngine;
using UnityEditor;
using System.Collections;
[ExecuteInEditMode]
[RequireComponent (typeof (MeshFilter))]
public class Generate : MonoBehaviour {
private Mesh s;
private Vector3[] vertices = {
new Vector2(0,0),
new Vector2(0,1),
new Vector2(1,1),
new Vector2(1,0)
};
private int[] triangles = {
0,1,2,
2,3,0
};
public void Create()
{
MeshFilter meshfilter = gameObject.GetComponent<MeshFilter>();
s = meshfilter.mesh; //Error Line//
s.name = "S";
s.vertices = vertices;
s.triangles = triangles;
}
}
Answer by karl_ · Jul 06, 2015 at 01:47 PM
Assuming your MeshFilter.sharedMesh field doesn't point to an already valid mesh, just assign s to it instead of the other way around.
GetComponent<MeshFilter>().sharedMesh = s;
Edit - full source:
using UnityEngine;
using UnityEditor;
using System.Collections;
[ExecuteInEditMode]
[RequireComponent (typeof (MeshFilter))]
public class Generate : MonoBehaviour
{
public Color FillColor;
private Mesh s;
private Vector3[] vertices = {
new Vector2(0,0),
new Vector2(0,1),
new Vector2(1,1),
new Vector2(1,0)
};
private int[] triangles = {
0,1,2,
2,3,0
};
public void Create()
{
s = new Mesh();
GetComponent<MeshFilter>().sharedMesh = s;
s.name = "S";
s.vertices = vertices;
s.triangles = triangles;
}
void Start()
{
Create();
}
void Update()
{
// if Create() hasn't been called yet, the mesh is still null
if(s == null)
return;
Vector3[] vertice = s.vertices; //Error Line//
Color[] colors = new Color[vertice.Length];
int i = 0;
while (i < vertice.Length)
{
colors[i] = FillColor;
i++;
}
s.colors = colors;
}
}
I had to modify the code to get it to work, but I am still getting an error?
using UnityEngine;
using UnityEditor;
using System.Collections;
[ExecuteInEdit$$anonymous$$ode]
[RequireComponent (typeof ($$anonymous$$eshFilter))]
public class Generate : $$anonymous$$onoBehaviour {
public Color FillColor;
private $$anonymous$$esh s;
private Vector3[] vertices = {
new Vector2(0,0),
new Vector2(0,1),
new Vector2(1,1),
new Vector2(1,0)
};
private int[] triangles = {
0,1,2,
2,3,0
};
public void Create()
{
s = GetComponent<$$anonymous$$eshFilter>().shared$$anonymous$$esh; //Fixed Line// :)
s.name = "S";
s.vertices = vertices;
s.triangles = triangles;
}
void Update() {
Vector3[] vertice = s.vertices; //Error Line//
NullReferenceException: Object reference not set to an instance of an object
Color[] colors = new Color[vertice.Length];
int i = 0;
while (i < vertice.Length) {
colors[i] = FillColor;
i++;
}
s.colors = colors;
}
}
I updated my answer with full source, you're just missing a constructor for the mesh s ( s = new $$anonymous$$esh() prior to assigning shared$$anonymous$$esh).
Thanks for all your help, but I am getting an error at Vector3[] vertice = s.vertices; //Error Line// NullReferenceException: Object reference not set to an instance of an object
Right, because you're not assigning s before using it. You need to add the s = new $$anonymous$$esh(); line before assigning it to the component, which again should be GetComponent<$$anonymous$$eshFilter>().shared$$anonymous$$esh = s; and not the other way around (as you have it). I updated my answer with a working example, make sure to pay attention to the order of assignments in Create(). Also note that you have to assign s to something valid prior to entering the Update method, otherwise you'll be accessing a null mesh. If you can't be guaranteed that s will be created prior to Update, consider adding a simple if(s == null) return; statement at the start of your Update function.
I am now not getting any error's but my initial intension was to not use shared mesh as I would like to set the duplicate to another colour.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Flip over an object (smooth transition) 3 Answers