Search Unity

Resolved Changes in Prefab done in Play Mode being applied to disk.

Discussion in 'Prefabs' started by diegopetrola, Jul 4, 2020.

  1. diegopetrola

    diegopetrola

    Joined:
    Feb 29, 2020
    Posts:
    4
    Hello everyone,

    I have a mesh imported from blender as .fbx. I made it into a prefab and attached a script that alters the mesh in the following away, (this script runs only in Play Mode).
    Code (CSharp):
    1. //First I tryed to just drag the prefab into the Hierarchy and work with it,
    2. //it didnt work either, I tryed this step out of desperation
    3. prefab = Instatiate(myPrefab);
    4.  
    5. Mesh mesh = prefab.GetComponent<MeshFilter>().mesh;
    6. Vector3[]  vertices = mesh.vertices;
    7.  
    8. /* do stuff with the vertices */
    9.  
    10. mesh.vertices = vertices; //I am doing this following Unity's documentation
    Problem is, after doing this, the mesh prefab and the imported .fbx are also changed. I also noticed a big drop in performance, probably because Unity is acessing the disk while these changes are being done in the RAM.
    I dont want this to happen, I want my changes done only in the instance of the object (in the RAM) and my prefab on the disk to be preserved.

    If I create my object in Unity's ProBuilder everything works fine, but I want to do it with an imported object for the sake of learning.

    Any advice is greatly appreciated :)
     
  2. diegopetrola

    diegopetrola

    Joined:
    Feb 29, 2020
    Posts:
    4
    When trying to update the MeshCollider the "sharedMesh" property was pointing to the mesh on the disk, that's what was causing my troubles. I fixed it now.