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 22, 2015 at 04:13 PM · mesharrayvector3vertices

Assigning new Mesh Vertices

Hi Unity Community

I having issues with some code where I am trying to assign new values from Vector3 array to that of the vertices in the object's mesh.

For my needs I could just delete the existing object in scene and create a new one but that isn't really solving the issue of my logic so could anyone please provide me some pointers as to where I am going wrong?

Many thanks in advance, Ryan

 function ExpVertText(userText : String)
 { 
     var textArray = userText.Substring (1, userText.Length-2).Split ([")("], System.StringSplitOptions.RemoveEmptyEntries);
     var vectArray = new Vector3[textArray.Length];
      for (var i = 0; i < textArray.Length; i++) {
         var numbers = textArray[i].Split(","[0]);
         vectArray[i] = Vector3(parseFloat(numbers[0]), parseFloat(numbers[1]), parseFloat(numbers[2]));
         }
     Debug.Log("Mesh Stored");
         
     var curObj = GameObject.Find("TestObject"); //current object
     var curVertArray = curObj.GetComponent(MeshFilter).mesh.vertices;
     
     
     for (var j = 0; j < vectArray.Length; j++) {
         Debug.Log("Old vert: " + curVertArray[j]);
         curVertArray[j] = vectArray[j];
         Debug.Log("New vert: " + curVertArray[j]);
     }
     
     Debug.Log("Mesh Updated");
     return curObj;
 }
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

2 Replies

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

Answer by maccabbe · Apr 23, 2015 at 01:32 AM

Mesh.vertices returns a copy of the vertex positions or assigns a new vertex positions array. http://docs.unity3d.com/ScriptReference/Mesh-vertices.html

When you assign the vertices to curVertArray you are assigning them to a copy of Mesh.vertices. You need to assign curVertArray back to Mesh.vertices after you are done editing it, i.e. add the following line at line 20

 curObj.GetComponent(MeshFilter).mesh.vertices=curVertArray;
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 26, 2015 at 10:42 PM 0
Share

@maccabbe

you are a legend; this was my very problem, thank you very much!

avatar image
1

Answer by Graham-Dunnett · Apr 22, 2015 at 04:15 PM

Try the examples provided here:

http://docs.unity3d.com/ScriptReference/Mesh.html

I believe the documentation is correct.

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 23, 2015 at 01:04 AM 0
Share

Hi @Graham Dunnett

Thank you very much for your prompt reply! Yes the mesh reference is great, I used it frequently to get to my current point.

The issue I'm encountering is that I need to iterate through the arrays for the length of the shorter Vector3[] array (derived from text based information) and replace some of the mesh.vertice array with its contents.

The docs suggest to clear the mesh before assigning new vert values however, I only wish to partially change the existing mesh, so calling Clear() does't seem like an option. I assumed that by iterating for the length of the short array and assigning the new vertices values would achieve this, however I can't seem to save these to the .mesh object.

It seems to work when declared as newVectArray = oldVectArray which would replace the whole array, however I can't get this to work when iterated through as newVectArray[i] = oldVectArray[i]. Can you please shed some light as to why this is the case?

Thank you very much in advance, 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

2 People are following this question.

avatar image avatar image

Related Questions

Most Optimal way of computing Delaunay Triangulation given an Array of Vector3 points 0 Answers

How to calculate normal direction for shared vertex position 1 Answer

Procedural Mesh Generation - Split Arrays into sections 1 Answer

Normalizing a cube made up of 24 meshes into a Sphere 0 Answers

Best Way to Link Vertices into Mesh 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