Instantiate Original

I have a GameObject prefab that I Instantiate into my scene. It Instantiates a different GameObject (sometimes multiple) into the scene as a child. The object also can Instantiate another instance of itself. The issue is when I Instantiate the instance of itself it also instantiates the other if previously instantiated. It basically uses the current object, and not the original prefab.

Here’s a pseudo example:

public class MyObject : MonoBehaviour
    {
               public MyObject myObject; //Prefab from editor
               public OtherObject otherObject; //Prefab from editor

               MyObject link; //Storage for Instantiated MyObject
               OtherObject otherLink; //Storage for Instantiated OtherObject

               void AddMyObject()
               {
                       link = Instantiate(myObject, new Vector3(0, 0, 0), Quaternion.identity, this.transform);
               }

               void AddOtherObject()
               {
                       otherLink= Instantiate(otherObject, new Vector3(0, 0, 0), Quaternion.identity, this.transform);
               }
    }

If I call AddOtherObject() then call AddMyObject() my new instance of MyObject contains an instance of the otherObject. It’s using the current MyObject instead of the prefab. I need it to use the prefab without the OtherObject instanced.

In the editor it says Gutter(Clone)(GutterScript) it should say Gutter(GutterScript) and works if I edit the value in the editor.

Any ideas?

193542-instanceerror.jpg

I figured out a workaround. I created a separate class that contained code to add MyObject and returned that instance to my first object.

public class OtherClass : MonoBehaviour
{
          public MyObject myObject; //Prefab from editor

          public MyObject AddMyObject()
          {
                    return Instantiate(myObject, new Vector3(0, 0, 0), Quaternion.identity);
           }
}

public class MyObject : MonoBehaviour
     {
                public OtherObject otherObject; //Prefab from editor
                OtherClass oc;

 
                MyObject link; //Storage for Instantiated MyObject
                OtherObject otherLink; //Storage for Instantiated OtherObject
 
                void AddMyObject()
                {
                        link = oc.AddMyObject();
                }
 
                void AddOtherObject()
                {
                        otherLink= Instantiate(oc, new Vector3(0, 0, 0), Quaternion.identity, this.transform);
                }
     }

Then I just grab the OtherClass from Unity somewhere Start or Awake

oc = GameObject.Find("OtherClass ").GetComponent<OtherClass >();

This works. Kind of roundabout, but it works.And always give me a copy of the original Prefab.