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 DenizCetinalp · Apr 30, 2014 at 02:11 AM · c#meshverticesmeshfilter

Get all vertices in gameObject to array.

This seems to work if the gameObject is a mesh itself:

 Vector3[] objectVerts = myGameObject.GetComponent<MeshFilter>().mesh.vertices;

However if the mesh is a child of an empty gameObject, it doesnt work since the gameObject itself has no MeshFilter.

So I figured this should work:

 Vector3[] objectVerts = myGameObject.GetComponentsInChildren<MeshFilter>().mesh.vertices;

but I get the error 'System.Array' does not contain a definition for 'mesh' and no extension method 'mesh' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)

Comment
Add comment · Show 3
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 robertbu · Apr 30, 2014 at 02:18 AM 1
Share

Your line of code looks fine and compiles fine for me. The only difference is that I used 'gameObject' rather than 'myGameObject'. How is 'myGameObject' declared and initialized?

avatar image DenizCetinalp · Apr 30, 2014 at 02:57 AM 0
Share

Oops, thats a typo. It should be GetComponentsInChildren (which gets all), ins$$anonymous$$d of GetComponentInChildren (this works but it doesn't get all the vertices).

avatar image robertbu · Apr 30, 2014 at 03:10 AM 0
Share

GetComponentsInChildren() returns an array. You will need to get the vertices from each entry in the array in some sort of loop. I don't know if you can process each array entry separately, or you might need to compile all the vertices into a single list. Without context it is hard to suggest specific code.

1 Reply

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

Answer by robertbu · Apr 30, 2014 at 04:24 AM

GetComponentsInChildren() returns an array, so if you want a single list of all the vertices, the individual vertices need to be combined from each of the mesh filters.

First approach: use a Generic List. Here is a function. You will need to put this at at the top of the file:

  using System.Collections.Generic;

And here the the function:

 Vector3[] GetVerticesInChildren(GameObject go) {
     MeshFilter[] mfs = go.GetComponentsInChildren<MeshFilter>();
     List<Vector3> vList = new List<Vector3>();
     foreach (MeshFilter mf in mfs) {
         vList.AddRange (mf.mesh.vertices);
     }
     return vList.ToArray ();
 }

A second approach is to use LinQ. Put this at the top of the file:

  using System.Linq;

Then the follow statement builds the lists:

 Vector3[] v = gameObject.GetComponentsInChildren<MeshFilter>()
         .SelectMany(mf => mf.mesh.vertices)
         .ToArray();

Note for any future people looking at this answer, GetComponentInChildren() returns the components on the parent as well as all the children. For this question it does not matter since the parent is an empty game object.

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

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

20 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

Related Questions

C# Preserving GameObjects' Previous Meshes 1 Answer

Is it possible to make vertices defined by a mesh and mesh filters animating? 0 Answers

How to Change an Object's Mesh to another Object's Mesh via Code C# 1 Answer

MeshCollider is not getting updated when adding vertex to mesh 1 Answer

C# Array of OtherArray's Meshes 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