buckybuck
buckybuck

Reputation: 25

Vector3.z initialize as a positive but then go negative without any script modification

I'm trying to build some hexagonal mesh and, as I was just trying to visualize with OnDrawGizmos some minimalist hexagon with 7 Vector3 vertices, something weird happened, because only five appears, missing the two from the top. And if I delete the last two vertices, the two top-ones show up immediately.

So I wrote in console the value of all my vertices and surprise, the first two have the same value as the last two, althought they aren't the same when I declare them :

hexVertices[0][0] = new Vector3(-outR/2f, 0, innR);
hexVertices[0][1] = new Vector3(outR/2f, 0, innR);
hexVertices[1][0] = new Vector3(-outR, 0, 0);
hexVertices[1][1] = new Vector3(0, 0, 0);
hexVertices[1][2] = new Vector3(outR, 0, 0);
hexVertices[2][0] = new Vector3(-outR / 2f, 0, -innR);
hexVertices[2][1] = new Vector3(outR / 2f, 0, -innR);

Which gives me :

(-0.5, 0.0, -0.9)
(0.5, 0.0, -0.9)
(-1.0, 0.0, 0)
(0.0, 0.0, 0.0)
(1.0, 0.0, 0.0)
(-0.5, 0.0, -0.9)
(0.5, 0.0, -0.9)

...Altought the first two ones should have a positive z. I first thought that, as my vertices are store in three arrays which I merge after declaring the vertices, maybe the problem's there, but it isn't. So I just wrote the value of my two vertices between every line to see where that inversion occurs, and it seems that when hexVertices[2][0] is declared, then hexVertices[0][0].z is inverted, and the same is for hexVertices[0][1].z when hexVertices[2][0] is declared.

Is this some kind of weird memory issue or am I totally missing something there ?

Upvotes: 2

Views: 61

Answers (1)

Ruzihm
Ruzihm

Reputation: 20259

It seems like you have it solved but I'm going to go ahead and write an answer so the question can be marked solved.

A good rule of thumb is to use one new for each independent copy you want to make.

Separating the line into two will fix your problem:

hexVertices[i] = new Vector3[nbVerticesMin + i];
hexVertices[nbLines - 1 - i] = new Vector3[nbVerticesMin + i];

Upvotes: 1

Related Questions