Jump to content
Sign in to follow this  
Hertzole

Duplicating a PB object at runtime results in same mesh, which causes problems

Recommended Posts

Hello!

I'm currently developing a level editor and I noticed whenever I duplicate a ProBuilder object, the mesh that gets created stays the same, which I believe causes some problems for me. If I delete any of the objects that have the same mesh, the mesh gets destroyed and all the other objects loose the reference to it and become blank.

Currently, this is the code I use to duplicate objects (very stripped down):

public void DuplicateObject(GameObject target)
{
    if (target == null)
        return;

    GameObject go = Instantiate(target, target.transform.position, target.transform.rotation);
}

I know what the problem is: I instantiate a new object based on "target", which will have all the same properties. And unfortunately, I can't change this.

So the main questions is, is there some way I can create a new instance of the mesh for newly created PB objects?

Share this post


Link to post
Share on other sites

All you need to do is call MakeUnique on the duplicated pb_Object.  That will copy the source mesh and make the new one independently editable.

pb_Object pb = GameObject.Instantiate(target).GetComponent<pb_Object>();

pb.MakeUnique();

 

Share this post


Link to post
Share on other sites
1 minute ago, karl said:

All you need to do is call MakeUnique on the duplicated pb_Object.  That will copy the source mesh and make the new one independently editable.

Oh, so it was that easy. Thanks a ton, karl! :D

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×