Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RyanAchtenSoma · Apr 07, 2015 at 10:35 AM · javascriptvariablefunctionverticesreferencing

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.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jcv8000 · Apr 07, 2015 at 12:10 PM 1
Share

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.

avatar image RyanAchtenSoma · Apr 07, 2015 at 12:36 PM 0
Share

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 ;)

avatar image Baste · Apr 07, 2015 at 12:53 PM 1
Share

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.

avatar image RyanAchtenSoma · Apr 07, 2015 at 10:56 PM 0
Share

Hi Baste

Thanks for that, I had read about this in other forum posts, will revise the code accordingly.

Cheers!

avatar image RyanAchtenSoma · Apr 08, 2015 at 06:14 AM 0
Share

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/

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RyanAchtenSoma · Apr 07, 2015 at 11:02 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

What is this C# code in javascript 0 Answers

variable = true from another script 2 Answers

passing a javascript array into a function sorts it? 1 Answer

How to call a variable that was defined in FixedUpdate in javascript 1 Answer

function CheckPassword():boolean{ is void? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges