Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Mesh duplicate has wrong position?

Discussion in 'Scripting' started by TTuPC, Apr 23, 2022.

  1. TTuPC

    TTuPC

    Joined:
    Mar 22, 2021
    Posts:
    36
    I'm trying to duplicate a mesh and do some operations on it, but once I duplicate it, I have a wrong position on screen:

    upload_2022-4-23_11-11-10.png
    Where the left one is the original one, and the right one the duplicate. I don't know why, but the transform says that the position is 0,0,0, but the mesh is there!
    This is my code for duplication:
    Code (CSharp):
    1.     void SplitMesh()
    2.     {
    3.         Mesh meshDuplicate = mesh;
    4.         GameObject duplicate = new GameObject();
    5.         duplicate.AddComponent<MeshFilter>();
    6.         duplicate.AddComponent<MeshRenderer>();
    7.         duplicate.GetComponent<MeshRenderer>().material = gameObject.GetComponent<MeshRenderer>().material;
    8.         duplicate.GetComponent<MeshFilter>().mesh = meshDuplicate;
    9.  
    10.     }
    What can I do?
     

    Attached Files:

  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,528
    I don't quite get the problem here. This line:
    Code (CSharp):
    1. Mesh meshDuplicate = mesh;
    does NOT create a duplicate of the mesh at all. You're using the exact same mesh.
    You haven't shown where the pivot of the original mesh is. How about selecting the original object? Because it looks like the vertices are just offset like this inside the mesh. In addition it looks like your other object is rotated 180° around y, but it's hard to tell from the image.
     
  3. TTuPC

    TTuPC

    Joined:
    Mar 22, 2021
    Posts:
    36
     
  4. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    777
    Hi there,

    Why don't you compare the gameobject properties, and parent, child relations while running the game in the Editor? And go from there by reason about how it relates to any differences you notice, and what may cause it.