Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Stitchey_ · Dec 16, 2015 at 10:20 PM · c#raycastmesherror message

Creating a custom mesh - Cleaning the mesh failed

I am pretty new to understanding meshes, and I'm creating a custom mesh using code, and I'm getting an error I have no idea how to solve, or what it even mean. alt text

"Cleaning the mesh failed".

What i'm trying to do is to create a mesh with the vertices of an array of raycasts, and put a mesh collider on it to signify an enemies vision.

This is my attempt of an illustration: alt text

And here's my current code, of course:

     public void CreateMeshBetweenPoints(Vector3[] points) {
 
         Mesh mesh = new Mesh();
 
         int[] triangles = new int[points.Length];
         int nr = 0;
 
         mesh.vertices = points;
 
         // Create the happy little triangles.
         for (int i = 0; i < ((points.Length) / 3); i++) {
 
             triangles.SetValue(0, nr);
             nr++;
 
             for(int j = 0; j < 2; j++) {
 
                 triangles.SetValue((j+ 1) + i, nr);
                 nr++;
 
             }
 
         }
 
         for(int k = 0; k < triangles.Length; k++) {
 
             print (triangles[k] + " :: [" + k +"]");
 
             if(k == triangles.Length)
                 print (triangles[k+1] + " :: [" + k+1 +"] + !!" );
 
         }
 
         mesh.triangles = triangles;
 
         /*
         print ("tri: "+ triangles.Length);
         print ("vert: "+mesh.vertices.Length);
         print ("p: " + points.Length);
          */
         detectionCollider.sharedMesh = mesh;
 
     }

Thank you for your attention. Please tell me what I'm doing wrong, or what I should do instead. Also leave a comment if you need more information.

Thanks!

helpunity.jpg (53.8 kB)
untitled.png (16.8 kB)
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 Cherno · Dec 16, 2015 at 11:35 PM 0
Share

Try the Clear() function before assigning the generated mesh, as in the example of the Scripting API.

  detectionCollider.shared$$anonymous$$esh.Clear();
 detectionCollider.shared$$anonymous$$esh = mesh;

avatar image Stitchey_ Cherno · Dec 17, 2015 at 10:55 AM 0
Share

It tells me "object reference not set to an instance of an object" refering to the "$$anonymous$$esh.Clear" function. The missing object reference might be because the mesh it is trying to clear does not excist, as it isn't set to anything when it's trying to clear it. I don't see why this would be a problem, though.

avatar image Cherno Stitchey_ · Dec 17, 2015 at 03:01 PM 0
Share

In that case, check if the mesh is null.

 if( detectionCollider.shared$$anonymous$$esh != null) {
      detectionCollider.shared$$anonymous$$esh.Clear();
 }
 
  detectionCollider.shared$$anonymous$$esh = mesh;

Double click on the $$anonymous$$esh in the inspector in the $$anonymous$$eshFilter component to see a 3d view of the generated mesh, if it indeed worked. That way, you can at least see if the mesh was assigned, even if it doesn'T show up in the scene view for some reason.

2 Replies

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

Answer by Stitchey_ · Dec 20, 2015 at 02:26 AM

I found out what was wrong, and got it working. The "Cleaning the mesh failed" thing was as pointed out by Cherno (Thank you!) caused by not using the Mesh.Clear() function before setting a mesh, but surprisingly that seems only to be the case with the mesh collider, and this is where I get in to the bigger problem that was present. By using the mesh collider instead of a mesh renderer there were a lot of problems, as instead of wanting a mesh, the mesh collider wanted a shared mesh, which I still don't know the differences of, by the way. By short term, don't use a mesh collider like I did, and come up with a different and better solution.

Here's my current code, which I may mess around with to make more efficient in the future, but apart from that, It's working fine:

 MeshFilter mf;
     mf = detectionMeshHolder.GetComponent<MeshFilter> ();

 public void CreateMeshBetweenVertices(Vector3[] vertices) {

     Mesh mesh = new Mesh ();
     int[] triangles = new int[vertices.Length*3]; // The triangles of the mesh.
     Vector2[] uvs =  new Vector2[vertices.Length]; // The uvs of the mesh.
     int nr = 0; // A number to show the iteration number between both for loops.

     for (int i = 0; i < vertices.Length -2; i++) {
         nr++;
         triangles.SetValue(0, nr); // For every 3rd integer of the triangle array, set it to 0.

         // Set triangles to 0,1,2,0,2,3,0,3,4,0,4,5... etc.
         for(int j = 0; j < 2; j++) {
             triangles.SetValue(j+i+1, nr);
             nr++;
         }

         uvs.SetValue(new Vector2(vertices[i].x, vertices[i].y), i); // Set the UV coordinates.
     }

     // Assign the mesh attributes.
     mesh.vertices = vertices;
     mesh.triangles = triangles;
     mesh.uv = uvs;

     // Assing the mesh.
     mf.mesh = mesh;

 }
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 Harinezumi · Jun 20, 2017 at 08:12 AM 1
Share

Interesting that you mention that you need to use $$anonymous$$esh.Clear(), but then in the example you don't use it. Why does it work then?

avatar image
0

Answer by CMCmaster · Mar 04, 2017 at 04:31 PM

You have a section where ALL Vertices OVERLAP and that is messing up cleaning procedures!!! Check for this particular case and do not set the collider if neaded

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 CMCmaster · Mar 04, 2017 at 04:22 PM 0
Share

A $$anonymous$$eshSection not a continuous line in the vertex array

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

44 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

Related Questions

Distribute terrain in zones 3 Answers

How to place a GameObject with mesh on a mesh "perfectly" through c# script? 1 Answer

How to snap a Prefab to the normal of an irregular mesh 1 Answer

Get forward direction of face hitted by Raycast. 1 Answer

How to Instantiate a prefab on the surface of a curved structure or mesh 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