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 Pietie · Jan 29, 2014 at 06:48 PM · arrayfor

Array index is out of range

I have one question, I have the script to edit all children in the object, from second object. Both objects have the same number of children, of course, the same material ... but after running I got error "Array index is out of range." It's strange, because if I set the same object for regular and smoothed, here's no error as well if objects have only one child ... I tried foreach instead of for, but it did not help ... Any ideas?

     public GameObject regularMesh;
     public GameObject smoothedMesh;
 
     private float ScaleAndBias(float normal)
     {
         return (normal * 0.5f + 0.5f);
     }
 
     void  Start (){
         MeshFilter[] allChildren = new MeshFilter[regularMesh.transform.childCount];
         allChildren = regularMesh.GetComponentsInChildren<MeshFilter>();
         MeshFilter[] allChildren2 = new MeshFilter[regularMesh.transform.childCount];
         allChildren2 = smoothedMesh.GetComponentsInChildren<MeshFilter>();
 
         for (int m = 0; m < allChildren.Length; m++){
 
             Mesh m1 = allChildren[m].mesh;
             Mesh m2 = allChildren2[m].mesh;
 
             GameObject go = new GameObject(allChildren[m].transform.name);
             go.transform.parent = this.transform;
 
             MeshFilter meshFilter;
             meshFilter = go.AddComponent<MeshFilter>();
 
             MeshRenderer meshRenderer;
             meshRenderer = go.AddComponent<MeshRenderer>();
             meshRenderer.materials = allChildren[m].renderer.materials;
             Mesh newMesh = new Mesh();
             meshFilter.mesh = newMesh;
 
         Color[] newColors = new Color[m1.vertexCount];
 
         for (int i = 0; i < m1.vertexCount; i++)
         {
             newColors[i] = new Color(ScaleAndBias(m2.normals[i].x), ScaleAndBias(m2.normals[i].y), ScaleAndBias(m2.normals[i].z)); 
         }
 
         newMesh.vertices = m1.vertices;
         newMesh.triangles = m1.triangles;
         newMesh.normals = m1.normals;
         newMesh.uv = m1.uv;
         newMesh.colors = newColors;
 
         }
 
         //ObjExporter.MeshToFile (meshFilter, Application.dataPath + "//Test.obj", true);
     }
Comment
Add comment · Show 2
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 VildNinja · Jan 29, 2014 at 06:54 PM 0
Share

on which line did you get the error?

avatar image Pietie · Jan 29, 2014 at 06:56 PM 0
Share
 newColors[i] = new Color(ScaleAndBias(m2.normals[i].x), ScaleAndBias(m2.normals[i].y), ScaleAndBias(m2.normals[i].z)); 

1 Reply

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

Answer by VildNinja · Jan 29, 2014 at 07:11 PM

m2 has fewer vertices than m1. You are using m1 vertex count to iterate m2.

 for (int i = 0; i < m1.vertexCount; i++)
 {
     newColors[i] = new Color(ScaleAndBias(m2.normals[i].x), ScaleAndBias(m2.normals[i].y), ScaleAndBias(m2.normals[i].z)); 
 }

I assume that your smooth model is smoothed from a 3D program. That means that a lot of the vertex can be reused for several polygons, as they now have a shared normal. Where none smooted models have three unique vertex per triangle. Your polycount may still be the same, but the vertex and normal count can differ.

Comment
Add comment · Show 16 · 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 Pietie · Jan 29, 2014 at 07:29 PM 0
Share

For smoothing, I use this in Unity.

alt text

This means that just change m1.vertexCount to m2.vertexCount?

výstřižek.jpg (13.7 kB)
avatar image VildNinja · Jan 29, 2014 at 07:36 PM 0
Share

Well yes, to fix that error.

But I am not quite sure what you are trying to do. you want the normals from m2 to be the colors of m1? You can't do it like that because m1 have more vertices than m2, so you will have to few colors.

Just to be perfectly clear m1 and m2 are from the exact same model right? m2 is just smoothed?

avatar image Pietie · Jan 29, 2014 at 07:46 PM 0
Share

Yes, it is the same model. I want to do this:

http://answers.unity3d.com/questions/625968/unitys-outline-shader-sharp-edges.html#answer-626001

It is working, but only for one full object with one $$anonymous$$esh Filter, like box, cylinder,... not for objects with children (it is actually almost all objects)

avatar image VildNinja · Jan 29, 2014 at 07:53 PM 0
Share

Hmm make sure that m1.triangles.Length is the same as m2.triangles.Length and that m1.vertex.Length is the same as m2.vertex.Length. The most important thing is that triangles.Length are the same.

avatar image Pietie · Jan 29, 2014 at 08:05 PM 0
Share

Lenght of triangles is the same - 240, but m1 have 99 vertices and m2 87... What to do?

Show more comments

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

18 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

Related Questions

[solved] passing energy from one object to another 0 Answers

Generic list of objects, then distance to those objects? (js) 1 Answer

print(var a = object) = null while print(oject) = what i want 2 Answers

How to store gameobjects in an array 2 Answers

How do I check if all objects in an array are destroyed? 2 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