constructive advice needed for using builtin arrays and programming in general

Foreward : While I appreciate all input, for this one question I am looking for decisive, precise answers rather than vague suggestions or open-ended comments. I also understand people may not want to answer my question, as it holds some elements to unlocking the keys of the Unity universe, and that’s where some people make their bread and butter. I am not trying to ruin anyones livelyhood, merely want to make these things myself to use myself to expand my knowledge and save money I don’t actually have =]

This stems from what I thought was a simple question. Basically I had a mesh script that worked with Unity 3.5.1, but when I went to 3.5.6 I started getting an error. Simple question. The details are I have a text mesh (meaning a quad is created per character in the string), when the length of that string changed, and on the first time that text mesh array length was changed, I would get an out of bounds error. However on the next construction of the mesh there was no error and the mesh worked as expected.

This is a simple problem I thought, and went through finding where the lengths of the arrays were assigned, made sure they were not being fed too much or too little information, and even doubly assigned the length of the mesh vertex info before assigning their values.

Here is the question I am speaking of, it has turned into a long mess, and totally irrelevant to the question I asked, only to receive “you are doing it wrong” : array out of range while updating mesh variables - Questions & Answers - Unity Discussions

I know Kryptos is correct (he usually is!) but I just cannot see it when I have working meshes with verts animated every frame (reading curves) and they work perfectly without errors or clearing. phodges also seems very knowledgeable, but as in my comment I believe he contradicted his initial statement leaving me confused.

I don’t believe so, and if I am wrong please leave a concise answer in a format I can follow and absorb, for example my method I have evolved into for creating my meshes is as follows :

// Variables
var verts : Vector3[];
var uvs : same for uvs, tris, normals

// Initialization
if(!mesh) { GetComponent(MeshFilter).mesh = mesh = new Mesh();}

verts = new Vector3[4];
verts[0] = new Vector3( 0, 0, 0);
verts[1] = same for all verts, uvs, tris, normals

mesh.vertices = new Vector3[verts.length];
mesh.uv = same for uvs, tris, normals

mesh.vertices = verts;
mesh.uv = same for uvs, tris, normals

mesh.RecalculateBounds();   
mesh.RecalculateNormals();

// Update
mesh = this.transform.GetComponent(MeshFilter).mesh as Mesh;

verts[0] = new Vector3( 0, 0, 0);
verts[1] = same for all verts

mesh.vertices = verts;

The pattern I have made for myself can be seen in my questions (all of these have been answered, thanks. The asteroid script has moved on quite alot infact!)

Here : simply assign tangents to single quad - Questions & Answers - Unity Discussions

Here : irregularities with procedurally generated sphere - Questions & Answers - Unity Discussions

I can provide the script for my animated ‘sock’ if required for reference also.

and on the script in my last comment Here : Calculate Spline points on Complex Geometry Mesh - Questions & Answers - Unity Discussions

my forum box is the same as my handle on UA., I am happy to share my full sprite and text classes with those here who are easily able to create better (to preserve their income by not displaying those type of ready to use scripts here). I know that doesn’t sound cool, but I do help alot of people out with scripts anyway, and what have I done attemting to quash all the ‘noob’ Slender questions with my guide?! But seriously I just want to get my text mesh and sprite class working for my own personal and commercial use, your constructive informative comments are much appreciated as always, thanks.

(1) well if the array is out of range, the array is out of range

extremely simple final solution:

use Debug.Log to print all the .Count or .Lengths involved of all sides and you’ll soon see what’s going on.

(2) BTW one thing that I found very confusing when first using unity,

were you aware that .vertices CREATES A COPY. it is completely different in use from every other (as far as I know) .property in Unity. You need to be totally clear on this.

(3) also did you know this:

so don’t use their recalculate normals unless you specifically want to. You have to be real clear on this.

calculating normals is an incredibly fine art. like, you could have a massive “AI” project to figure out normals well, in certain situations and types of problems. it is completely non-trivial, it’s like painting. the “RecalculateNormals” function is somewhat dangerous as it could make people think it’s a mechanical process. it’s only mechanical for say flat surfaces (even then how you treat the edges of reality is an issue). so think of Mesh.RecalculateNormals as a handy placeholder function for helloMesh projects to get you going, or a thing you use in very specific situations if it “just happens” to be what you need.

crib sheet …

// Mesh.RecalculateBounds - "Assigning triangles will automatically Recalculate the bounding volume." // Mesh.RecalculateNormals - unity's routine for normals, generally not used for custom meshes
// Mesh.Optimize - internally figures optimisations, eg vertex cache locality. could possibly be useful, often not
// note that GetComponent(MeshCollider) does indeed have a "mesh" property
// (not just a "sharedMesh") although undocumented

also (4) note the comment there…

“SUGGESTION: notice they use the same name for “their” normals (normals) as the property name (.normals). FWIW is usually ask people to use a different name. Same deal for verts. For example use, “ourNormals” or “theNewMoreBendyNormals” or “theSpecialRuffledVerts” or “myCurrentSockShape” or something self-documenting. it’s just much clearer if it’s not the same.”

(always write self-documenting code … and this is a good example. “if your code has comments, your variable names are too short”)

(5) as Eric mentions I’m not sure precisely what you’re asking?

(6) to repeat … if you’re struggling w/ the length of arrays. Just spend 2 seconds typing many Debug.Log statements to get the length of everything all over the place. could you be more clear about precisely what length is causing problems?

(7) one thing to consider. don’t forget that with mesh you can - if you want - “share” verts. do you completely and totally understand this process? there is no reason, whatsoever, that you have to share verts. Every single triangle-point entry can have it’s vertex-position entry (ie, length is identical). (of course, obviously if you want them to be in the same place, they’d have to be identical values. but there’s no real reason they have to be in the same place, the pipeline is happy to paint just completely separate triangles floating in space.) for tricky mesh, it is often HUGELY simpler to not share any verts. in some situations, I sort of do all the work NOT sharing them, because it’s so much easier. and then just as a final process, a script sweeps over and shares 'em. you should be comfortable with both approaches. my point is that may resolve your length issues.

(8) I"GetComponent(MeshFilter).mesh = mesh = new Mesh(); was just a small optimization of code to one line."

I would not in english describe that as “optimisation” since that word usually refers to, writing code in such a way that the final application runs faster. you should refer to that as “making the code shorter” or something similar. (Much like when you write a historic language called Perl - the national sport is being a smart ass in compact code.)

Regarding making code more compact. Never do it, ever. You don’t have to “save ascii”. In fact almost always, go for the more languid form.

(Regarding actual “optimisation.” Never ever do it, or think about it for any reason. As a minor subset of that rule: note that how you write code is utterly irrelevant and has been for ten - fifteen years. The compiler takes over and does what it wants.)

(9) cacheing … I think caching is probably the best spelling (although “eing” forms for newly minted words appeal to me generally)

(10) "remove the stigma " … did you mean stigma there? is that a c# term?