- Home /
Issues referencing variables between functions
Hi Unity community!
I am relatively new to Unity and the language which I am scripting in, Javascript, so please excuse any inadvertent naivety. I am having issues referencing variables between functions, and after much research, I thought it would be best to ask for help.
The case in which I am currently encountering this issue is in a script where I would like to be able to identify the vertex vectors of the designated mesh (in this case the test object is simply a cube) and allow the user to adjust these coordinates using the GUI text fields.
The code is as follows:
#pragma strict
private var menuArea : Rect;
private var menuAreaHidden : Rect;
private var mesh : Mesh;
static var vertVect : Vector3[];
var vertLength : int;
var vertAdjustField : String;
function Start ()
{
menuArea = Rect( 0, 0, Screen.width * 0.3, Screen.height * 0.9 );
menuAreaHidden = Rect( 0, 0, Screen.width * 0.3, Screen.height * 0.15 );
VertReport();
}
function Update ()
{
}
function VertReport()
{
var mesh : Mesh = GameObject.Find( "Cube" ).GetComponent.<MeshFilter>().mesh;
var vertVect : Vector3[] = mesh.vertices;
for (var i = 0; i < vertVect.Length; i++)
{
Debug.Log( vertVect[i] );
}
mesh.vertices = vertVect;
Debug.Log(mesh.vertexCount);
}
function OnGUI () {
GUI.Box( menuArea, "CubeTest01" );
GUILayout.BeginArea( Rect( menuArea.x + 10, menuArea.y + 30, menuArea.width - 20, menuArea.height - 50 ) );
GUILayout.BeginVertical();
var vertLength = vertVect.Length;
for (var i = 0; i < vertLength; i++)
{
new vertAdjustField = GUILayout.TextField( "v" + vertices[i] );
}
GUILayout.EndVertical();
GUILayout.EndArea();
}
This is my first post on UnityAnswers so apologies for any breaches in etiquette. Thanks very much in advance for any help you guys can provide me with.
Cheers, Ryan.
Try changing line 25 to
vertVect = mesh.vertices;
That's all I could find. If it doesn't solve anything, could you please tell us the error it gives you and on what line. If there's no error, tell us what is happening wrong.
Hi Crafterguy3x3
That definitely helped, thank you very much!! The core of my issue, which I should have made clearer, being able to reference the mesh.vertices defined in the VertReport function in the OnGUI function. Whenever I attempt to do this it will throw a null reference, despite my attempts to make this as public as possible.
I'm also encountering a few obscurities which are confusing me such as the vertice.Length of the cube returning 24 as opposed to my anticipated 8 which is messing with my counter etc.
If someone could help clarify these I would be eternally grateful!
FYI I realise textfield code on ln.42 is incorrect; still working on that part ;)
The vertice length of the cube should be 24.
This has to do with lighting - to have the corners react to light correctly, each face has to have four unique vertices, with their own normal and tangent values.
Hi Baste
Thanks for that, I had read about this in other forum posts, will revise the code accordingly.
Cheers!
Just to clarify for those who may stumble upon this post with the same issue, this was due to the cube's vertex normals. Because the 8 vertices in a cube each connect 3 faces, Unity and the GPU actually register the 3 vertex normals for each of these faces; i.e. 3 x 8 = 24 vertices.
For more information check out this excellent tutorial by CG Cookie https://cgcookie.com/unity/2013/04/18/understanding-gpu-vertex-count/
Answer by meat5000 · Apr 07, 2015 at 12:42 PM
Any variables declared inside a function are not visible outside of that function. It has no 'scope'.
You can declare your variables outside of your function. I understand the desire to declare variables inside functions so if you wish to read these variables you can create a global variable (Outside the functions) and use it to store the contents of the information you wish to use.
var myVar;
function MyFunc()
{
var newVar = stuff;
myVar = newVar;
}
myVar is just a holder for the information you wish to view outside the function.
Hi meat5000
Yes this was exactly my issue. I was referencing the .vertice function ins$$anonymous$$d of the global variable I had declared outside of the function to hold the vertice count.
Thank you so much, Ryan