Write your Instantiate code into the object’s start function. Everytime the object cloned, Start function will be invoked and new object will be created.
// Use this for initialization
void Start()
{
GameObject clone;
Transform parentEmptyObject = GameObject.Find("parentObject").GetComponent<Transform>();
if(Game.totalCube < 100)
{
clone = Instantiate(gameObject) as GameObject;
/// use clone to manipulate transform, rigidbody, rotation etc.
clone.GetComponent<Transform>().SetParent(parentEmptyObject);
}
}
I also added Game static variable to limit cloning process.
From your question, I’m going to assume that you’re trying to achieve the effect of:
One ‘rigid’ object that starts off small (but can still move around under physics)
Over time it can ‘grow’ by duplicating bits of itself
So maybe it starts off as 1 box, then that grows into 5 boxes, then they each grow a little bit etc. A bit like baceria growing.
if what you want is just a single box that gets bigger, then its very simple - just look at modifying the ‘transform.localScale’ property. I’m going to assume you’re after the cloning behaviour though.
So first, I would create 1 game object with a rigid body, and call this ‘ExpandingBlob’.
I would also create a game object with a box collider (just use a cube), which we’ll call ‘BodyPart’. Now drag that game object into the project to create a prefab from it.
Next, create a script on your expanding blob, and expose a public GameObject variable called ‘BodyPartPrefab’. Use the inspector to point this at the prefab we made earlier.
Now at any point your ExpandingBlob script has the ability to:
Instantiate an instance of your body part
Set the ‘transform.parent’ of the new instance to be itself (and thus part of the same rigid body)
Set its local position to wherever you want it
Hope that helps. I would start by making your ‘ExpandingBlob’ script just wait 1 second then spawn a single body part with a transform.localPosition of [0,0,0]. If you’ve got it right, this should appear at the exact centre of ExpandingBlob, and be part of its rigid body. From there you can experiment with spawning more body parts and choose where to spawn them.
Your problem is a bit too vague and big for me to write the code for you - plus where’s the fun in that But take that rough approach and you should be able to make a ‘growing blob’ in no time!