Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
4
Question by DaveM · Apr 05, 2010 at 08:01 PM · collidermeshprocedural

How to assign procedural mesh to a collider

How do I assign a procedural mesh to a collider? The mesh name that I assign in the script isn't listed in the Mesh drop down until I run the game. At that point I can pause the game, assign the mesh and everything works fine but I can't assign before running.

The code that I'm using now:

gameObject.AddComponent(MeshFilter); gameObject.AddComponent("MeshRenderer"); gameObject.AddComponent(MeshCollider);

var mesh : Mesh = GetComponent(MeshFilter).mesh; mesh.name = "terrain1"; GetComponent(MeshCollider).sharedMesh = mesh; <-- This doesn't work

Comment
Add comment · Show 1
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 Maxime · Jul 03, 2012 at 02:52 PM 0
Share

I am having a same problem, I can't find any help in the documentation.

6 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by qJake · Apr 22, 2010 at 09:06 PM

Assigning a Mesh Collider to a game object procedurally is relatively easy.

This will assign a Mesh Collider to the mesh. Note that this has to be done AFTER you make and apply your mesh, so stick it at the bottom!

// C# MeshCollider meshc = gameObject.AddComponent(typeof(MeshCollider)) as MeshCollider;

meshc.sharedMesh = terrain1; // Give it your mesh here.

That's it! Unity will calculate and apply your mesh collider here.

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 cregox · Dec 10, 2013 at 06:39 PM 0
Share

Probably not quite his issue, this is about the same of what he's already doing... Look at my answer.

avatar image
3

Answer by pkamat · Oct 05, 2010 at 09:48 AM

if you procedurally update the mesh collider u need to Null the mesh before assigning the new mesh. i dont know why but i think unity does some optimisation inside.

    GetComponent<MeshCollider>().sharedMesh = null;
    GetComponent<MeshCollider>().sharedMesh = meshFilter.mesh;

that should work

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 tingham · Feb 27, 2013 at 10:27 PM 0
Share

This didn't work.

avatar image Psyco92 · Jul 12, 2017 at 08:03 AM 0
Share
     GetComponent<$$anonymous$$eshCollider>().shared$$anonymous$$esh = null;
     GetComponent<$$anonymous$$eshCollider>().shared$$anonymous$$esh = meshFilter.shared$$anonymous$$esh;

Change the last line to work only with shared$$anonymous$$esh and it should work.

I have procedural mesh applied to meshFilter.mesh and after the mesh has been applied, I then apply the meshFilters shared mesh to the mesh colliders shared mesh.

avatar image
1

Answer by Jessy · Apr 05, 2010 at 10:52 PM

What you're trying to do with mesh.name doesn't work. I don't know where you're grabbing "terrain1" from, but here's what you'd do if it were in your project folder. (You'd drag it onto the variable slot. But if you were doing that, I don't see why you wouldn't just add the necessary components in the Editor. Regardless, this is the general idea.)

var terrain1 : Mesh;

(gameObject.AddComponent(MeshFilter) as MeshFilter).mesh = terrain1; gameObject.AddComponent(MeshRenderer); gameObject.AddComponent(MeshCollider);

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 cregox · Dec 10, 2013 at 06:42 PM 0
Share

Very well observed... When I read it, I thought he was just na$$anonymous$$g the mesh for w/e reason.

avatar image
1

Answer by DaveM · Apr 06, 2010 at 12:19 AM

Thanks for your help but I'm still not getting any collisions. I don't think that I can just drag from the project folder because the mesh isn't generated until runtime so there is nothing to drag.

Here's more of the code:

var terrain1 : Mesh; (gameObject.AddComponent(MeshFilter) as MeshFilter).mesh = terrain1; gameObject.AddComponent(MeshRenderer); gameObject.AddComponent(MeshCollider);

// Build vertices and UVs

........

// Assign them to the mesh

terrain1.vertices = vertices; terrain1.uv = uv;

// Build triangle indices

........

// And assign them to the mesh

terrain1.triangles = triangles; terrain1.RecalculateNormals();

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 Eric5h5 · Apr 06, 2010 at 12:53 AM 1
Share

It's a lot easier to read if you select your code and hit the code formatting button.

avatar image cregox · Dec 10, 2013 at 06:40 PM 0
Share

And it's a lot better if you only answer your question if you indeed have an answer. What you posted here would make more sense if you edited the question ins$$anonymous$$d.

avatar image
0

Answer by Ent · Oct 26, 2010 at 01:10 PM

Hi I am having a similar problem with my created meshes too. If I replace them with box colliders they work straight away. I am combining some meshes from single GameObjects together and so creating a new object.

heres my code:

public GameObject elementObj; public string modelName = ""; ...

this.elementObj = GameObject.Instantiate(Resources.Load("models/"+modelName),Vector3.zero,Quaternion.identity) as GameObject;

this.elementObj.AddComponent<MeshRenderer>();

Component[] meshFilters = elementObj.GetComponentsInChildren(typeof(MeshFilter)); CombineInstance[] combine = new CombineInstance[meshFilters.Length]; for (int i = 0; i < meshFilters.Length; i++) { combine[i].mesh = ((MeshFilter) meshFilters[i]).sharedMesh; combine[i].transform = ((MeshFilter) meshFilters[i]).transform.localToWorldMatrix; } Component frameBoxMesh = frameBox.GetComponent(typeof(MeshFilter)); ((MeshFilter)frameBoxMesh).mesh = new Mesh(); ((MeshFilter)frameBoxMesh).mesh.CombineMeshes (combine,true,true);

MeshCollider frameBoxColl = frameBox.AddComponent(typeof(MeshCollider)) as MeshCollider; frameBoxColl.sharedMesh = null; frameBoxColl.sharedMesh = ((MeshFilter)frameBoxMesh).mesh;

maybe someone else can say why such created meshes are not clickable or why mouse-over does not work. Like mentioned, if I replace the mesh in runtime with a box collider mesh, the scripts work instantly. Do these new created mesh have to be reloaded or something?

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
  • 1
  • 2
  • ›

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Box colliders from procedural mesh 1 Answer

Why attach a kinematic rigidbody to a collider that moves a lot? 1 Answer

Turn Off Mesh.Bake Scaled Mesh PhysX CollisionData ? 0 Answers

Breaking down Unity's Procedural Example "sculpt vertices" 1 Answer

Trouble with Inaccurate Mesh Collider 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