- Home /
Self Clone
So ya, I'm asking a question again.
The question is: I want to start an object in the middle of a place, then it will clone itself, after that the clone will clone itself again, etc.
The issue is: I can only instantiate one object, but not all. Plus, I want to clone a Rigidbody, not a gameObject
So, that's it, I plead for help.
In addition, can someone tell me how to make the clone objects stick together? I mean, it's like an object growing bigger.
A rigidbody must be on a gameobject for it to work.
First instantiate a prefab to make your first object, then the object can simply instantiate itself. This will duplicate the object. You already spelled it out really. This question is a little pointless :P
To stick obbjects together you can use parenting. $$anonymous$$ake one a child of the other and make the one you want following a $$anonymous$$inematic Rigidbody.
First, thnx for answering my question.
Ok, Do you mean that I need to do "Create Empty" for this to work? $$anonymous$$y actual problem is: only the object itself will instantiate, but the clones don't, I still don't know why.
On the other hand, what does the $$anonymous$$inematic do in the Rigidbody? I'm curious to know.
Anyway, if you can make a sample code for me, it's much appretiated, Whatever, thanks, bro.
$$anonymous$$inematic stops the object responding to physics in the normal way. It can still be scripted and collided. You dont need to do this. Its good for, say, sticking an object to a moving platform (with parenting).
First look up Instantiating from a Prefab. This is the first step. In a script on that object that is spawned you can simply Instantiate(gameObject
to duplicate it. No need for empty objects.
Answer by m0guz · Dec 09, 2015 at 09:19 AM
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.
Well, first, I can't see a static variable made, second.... I'm quite new here... So... I want you ins$$anonymous$$d help me 3 things.(Your script helps me a bit...)
I wanna know how a static variable is made, it doesn't work for me with all the resources in Google. Also I want to know how to reduce the lag if your script doesn't set a limit (I mean, it'll be cloned forever so it may exceed 10000 objects...). And lastly I wish you can tell me how to stick them all together in a position (Well, I don't even know what the parent-child thingy works-,-)
And, to say sorry, I'm using Javascript, so you may expect to explain in a more detailed way.
If you are new to Unity, you may want to check http://unity3d.com/learn/tutorials page first. It covers from basic operations like creating object, deleting to game logic.
Create a new script named Game, then add public int variable named totalCube to it. public static var totalCube : int = 0;
There is a good tutorial about statics https://unity3d.com/learn/tutorials/modules/intermediate/scripting/statics
There should be a limit, no matter what, but there is a way for unlimited, you need to clone object dynamically.
You can use clone.GetComponent()
to change position when cloning certain object.
Vector3 certainPos = GetComponent<Transform>().position;
GetComponent<Transform>().position = new Vector3(certainPos.x + 1, 0, 0);
I've watched this website already https://unity3d.com/learn/tutorials/modules/intermediate/scripting/statics
And I copy the exact same code into it but it didn't work.
btw, for the static variable, I don't know how to use them in other scripts, is it just use them normally as if they already exist in that script? Or is it I need to create another variable in that script to represent the static variable?
btw, the static variable is mainly for the use of counting the number of the objects and my health bar.
Answer by wibble82 · Dec 09, 2015 at 02:39 PM
Hey
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!
Well, I appreciate the comment you got, it's long. So, what I actually need...
Well, first, this is what it works:
1.It starts from 1 single object
2.Over time, it instantiates iteself(1,2,4,8...,131072,262144...) I hope u get it...
3.They're sticking together
4.They can be killed by a laser beam
5.After a certain amount (I expect to be about ~1000000)
So,I've done 1,2,4 but 3 and 5, problems are here:
3: I heard that the parent-child thingy will work, but I want to know how it actually works, so I can at least get use to it, too bad I don't rly know...
5: I used Rigidbody ins$$anonymous$$d of gameObject, it still works, Too bad that serious lag occur when exceed an amount I think it's mainly because it has a rigidbody component, so I wonder if removing the rigidbody thingy will work.
So, again, thnx for your help. If this is actually impossible, I may use your idea ins$$anonymous$$d.
PS: I'm using Javascript, so if u r using C#, you may expect to explain more... (I found that Javascript and C# has similar code...)