Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
  • Help Room /
avatar image
0
Question by burly87 · Jan 12, 2021 at 02:48 PM · scripting problemmeshprefabnullmeshfilter

Mesh missing in prefab

Hi, I'm trying to store informations about a Cube that is build out of 6 separate faces in a different GameObject. so I can have multiple versions of this cube, all with different parameters like color, size, resolution of each face etc. It is important that in the end I have n versions or copies of that one Object with totaly different values. The faces are generated as child Objects and just contains a Mesh Renderer and Mesh Filter component.

My first attempt was to make a prefab out of the existing cubeObject and then instantiate this prefab somewhere else (like on a placeholder transform in a different scene). When I create a prefab out of the existing cube the MeshFilter of each face is empty/ null.

another try was to think about saving all parameters somehow and then applying them to another cube. but I'm not sure how to do this in a proper way? maybe Making the hole thing a scriptable object OR I don't know.

The other core issue is if i dublicate the cube and then change the value of one of them ALL cubes get changed because of the way the cube is generated. Any suggestiones how to solve this would help a ton!

FACES.cs:

     private Mesh mesh;
     private int resolution;
     private Vector3 localUp;
 
     private Vector3 axisA, axisB;
 
     public Face(Mesh mesh, int resolution, Vector3 localUp)
     {
         this.mesh = mesh;
         this.resolution = resolution;
         this.localUp = localUp;
 
         axisA = new Vector3(localUp.y, localUp.z, localUp.x);
         axisB = Vector3.Cross(localUp, axisA);
     }
 
     public void ConstructMesh()
     {
         Vector3[] vertices = new Vector3[resolution * resolution];
         // getting nb of vertices; nmb of faces = (r-1)^2; *2 for triangles; *3 to get all vertices for every triangle 
         int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6];
 
         int triangleIndex = 0;
 
         for (int y = 0; y < resolution; y++)
         {
             for (int x = 0; x < resolution; x++)
             {
                 // index of vertices
                 int i = x + y * resolution;
                 Vector2 percent = new Vector2(x, y) / (resolution - 1);
                 Vector3 pointOnUnitCube = localUp + (percent.x - 0.5f) * 2 * axisA + (percent.y - 0.5f) * 2 * axisB;
                 // get vertices same distance to center of cube
                 Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
                 // calculate elevation and set them to the vertices
                 vertices[i] = pointOnUnitSphere;
 
                 // check if index is not outside the face
                 if (x != resolution - 1 && y != resolution - 1)
                 {
                     // first triangle, clockwise
                     triangles[triangleIndex] = i;
                     triangles[triangleIndex + 1] = i + resolution + 1;
                     triangles[triangleIndex + 2] = i + resolution;
                     // second triangle, clockwise
                     triangles[triangleIndex + 3] = i;
                     triangles[triangleIndex + 4] = i + 1;
                     triangles[triangleIndex + 5] = i + resolution + 1;
                     // update triangleIndex by 6 because we created 6 triangle
                     triangleIndex += 6;
                 }
             }
         }
         mesh.Clear();
         mesh.vertices = vertices;
         mesh.triangles = triangles;
         mesh.RecalculateNormals();
 
     }

CUBE.CS:

 public int resolution = 5;

 [SerializeField, HideInInspector]
 private MeshFilter[] meshFilters;
 private Face[] faces;


 void Initialize()
 {
     if (meshFilters == null || meshFilters.Length == 0)
     {
         meshFilters = new MeshFilter[6];
     }        
     faces = new Face[6];

     // all cardinal directions
     Vector3[] directions = { Vector3.up, Vector3.down, Vector3.left, Vector3.right, Vector3.forward, Vector3.back };


     for (int i = 0; i < 6; i++)
     {
         if(meshFilters[i] == null)
         {
             GameObject meshObj = new GameObject("mesh");
             meshObj.transform.parent = transform;

             meshObj.AddComponent<MeshRenderer>().sharedMaterial = new Material(Shader.Find("Universal Render Pipeline/Lit"));
             meshFilters[i] = meshObj.AddComponent<MeshFilter>();
             meshFilters[i].sharedMesh = new Mesh();                               
         }

         // generate Faces
         faces[i] = new Face(meshFilters[i].sharedMesh, resolution, directions[i]);
     }
 }

 void GenerateMesh()
 {
     foreach (Face face in faces)
     {
         face.ConstructMesh();
     }
 }

 private void OnValidate()
 {
     Initialize();
     GenerateMesh();
 }
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

0 Replies

· Add your reply
  • Sort: 

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

278 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 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 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 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 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 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 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

Why is my mesh not showing outside of an area? 0 Answers

Changing a mesh with C# and Resource.Load() 0 Answers

GetComponentsInChildren only works sometimes 0 Answers

Start() and Awake() not called on MonoBehaviour 1 Answer

How do I enable/disable components in a prefab from another script? 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