- Home /
Saving Certain Vector3's positions
I am working with editing meshes and I want to sort of stair step my edits. I want them to make very small changes and the vertices that are changed are saved in a new array that will then be changed again until the next step of editing array length is 0 and then it will stop. I have a script that edits but doesn't go past the very first step:
var firststep : Vector3[];
function Start() {
var mesh : Mesh = GetComponent(MeshFilter).mesh;
var vertices : Vector3[] = mesh.vertices;
var normals : Vector3[] = mesh.normals;
for (var i = 0; i < vertices.Length; i++)
{
number1 = Random.Range(0,1);
vertices[i] += normals[i] * number1;
if ( number1 > 0)
{
// here is where I want to add the changed vertices
firststep.Add(vertices[i]);
}
}
mesh.vertices = vertices;
print(firststep.Length);
}
so far though this always prints the firststep.Length as zero. It's not adding any of the changed vertices when I can clearly see them changing. Any ideas?
Answer by Eric5h5 · Jan 19, 2012 at 01:42 AM
You can't add anything to fixed-size arrays like Vector3[]. Use List instead.
I tried array lists and ran into all sorts of trouble. how would I do that? I was using this http://answers.unity3d.com/questions/17033/how-do-you-use-removeat-or-add-with-an-array-of-ga.html but I don't know if I was just doing it wrong or what.
No, List, not array lists. http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
so would it be public List firststep for my variable and then just firststep.Add(verticies[i]) ?
After playing with it I'm getting the error NullReferenceException: Object reference not set to an instance of an object $$anonymous$$eshEditing.Start () (at Assets/$$anonymous$$eshEditing.cs:25)
everytime number1 = 1. can I not just type firststep.Add(vertices[i])? I'm using IList by the way or is that not right?
Answer by Wolfram · Jan 19, 2012 at 01:20 AM
There are two versions of Random.Range(x,y), one for integers, and one for floats. You are passing two integers, so it will return an integer between x and y-1, which in your case is always 0. Make sure you pass two float values to that function, in which case it will return a float value within that range.
I don't use UnityScript, so I'm not sure about the syntax, but it should be something like
number1 = Random.Range(0.0,1.0);
You are right I forgot the last number is y-1 haha but it still doesn't add the arrays like i need. Eric5h5 is on the right track I think.