• 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 zachwuzhere · Nov 17, 2017 at 09:20 PM · vector3

What is the syntax for a jagged Vector3 to initialize it?

How can I set each vector3 array inside of the jagged vector3[][] to 64, with out manually initializing it like so...

     Vector3[][] verts = new Vector3 [ 4 ][] {      
           new Vector3[64],
           new Vector3[64],
           new Vector3[64],
           new Vector3[64]
     };
         
    //Vector3[][] verts = new Vector3 [ 4 ][64]; 
 
     void Update () {
            verts[2] = _Mesh[0].vertices; //access the third Vector3[] of vector 3's }

   



Comment
Add comment
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

1 Reply

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

Answer by Bunny83 · Nov 18, 2017 at 03:53 AM

Uhm, use a for loop? A jagged array is not "one thing". You have the outer array which is an array of arrays and you have the inner arrays which are referenced from the outer array. By default when you just create the outer array the elements of the outer array will be "null". So there's no way around creating those nested arrays "manually".

 var verts = new Vector3[4][];
 for(int i = 0; i < verts.Length; i++)
 {
     verts[i] = new Vector3[64];
 }

However i don't quite see the reason to initialize them when you seem to plan to replace / overwrite the array with the aray that is returned from _Mesh[0].vertices.

This line does not "access" the third array but it replaces / overwrites the array:

 verts[2] = _Mesh[0].vertices;

That means the array that was referenced previously by the 3rd element will now be garbage collected. It's not recommended to read Mesh.vertices every frame. Each time you read the vertices property you will create a new array which is filled with the vertex data of the mesh.

This is a question that is lacking some background information about what you actually want to achieve.

Comment
Add comment · Show 2 · 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 zachwuzhere · Nov 18, 2017 at 06:11 PM 0
Share

Ah, I figured I would have to loop through it, just didn't know if there was a way other than that. By access I meant to copy the vertices into the array so I can access them through the array later.

When You say it "replaces/overwrites the array", does that mean I don't need to initialize the inner arrays if it is just replacing it with the mesh's vertices?

avatar image Bunny83 zachwuzhere · Nov 18, 2017 at 11:30 PM 0
Share

Yes, exactly. The manually created arrays are pointless when you're going to store a different array in the elements.


$$anonymous$$eep in $$anonymous$$d that $$anonymous$$esh.vertices is not a direct access to the meshs vertex array. "vertices" is a property which has a "get" method. When you read this property Unity will actually create a new unique array.

 Vector3[] arr1 = some$$anonymous$$esh.vertices;
 Vector3[] arr2 = some$$anonymous$$esh.vertices;
 if (arr1 == arr2) // this will ALWAYS be "false"

Both arrays arr1 and arr2 contain the exact same Vector3 values but they are two seperate arrays.


$$anonymous$$icrosoft has a decent documentation on jagged arrays so you might want to check it out.


Also keep in $$anonymous$$d that the vertices array that you get from reading the property has not "connection" to the actual mesh. So changing elements in that array won't change the mesh. You would need to assign the array back to the property in order to "set" the vertices.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

89 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make Vector Position Of Character? 1 Answer

help with movement along x axis and slow down. please help 1 Answer

Get a var's value plus X without modifying? 1 Answer

Weird problems with physics when a ragdoll is added to player model 2 Answers

making a camera that moves depending on mousepos.y 0 Answers


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