How would I make a script that would make it’s object that it is applied to duplicate itself every (variable amount of seconds)? I’ve tried this many times in different ways, but it hasn’t produced any reaction yet. Help appreciated. thanks.
Hi Boo,
They will duplicate themselves exponentially if you duplicated the item that the script is attached to, Is that what you want it to do?
IE:
Obj_01 duplicates and makes Obj_02
Obj_02 inherits the duplicate script too, so…
Obj_01 duplicates and makes Obj_03
Obj_02 duplicates and makes Obj_04
03 and 04 inherit the script so,
Obj_01 duplicates and makes Obj_05
Obj_02 duplicates and makes Obj_06
Obj_03 duplicates and makes Obj_07
Obj_04 duplicates and makes Obj_08
etc…
Is this what you’re looking to do… otherwise I would suggest making an empty game object and duplicating it from that by assigning a GameObject that you want to duplicate from there.
Or, make sure you destroy the component after instantiating.
For the first method I mentioned you would write the code as something like this and attach it to an empty game object and assigning the object you want to duplicate in the inspector:
*** Untested Code ***
var duplicationObject : GameObject;
var frequency : float = 1.0; // Will duplicate every x seconds
private var nextDuplicationTime : float;
function Update() {
if (Time.time > nextDuplicationTime) {
Instantiate(duplicationObject);
nextDuplicationTime = Time.time + frequency;
}
}
function Start() {
nextDuplicationTime = Time.time + frequency;
}
Probably simpler just to use InvokeRepeating.
–Eric
Great script, but I was thinking about the whole “duplicate exponentially” thing and I guess I need it to duplicate itself twice, then stop duplicating and let the duplicates do the same. Like it’s running out of juice, or something. Thanks for the help with this, tho.
Well you may want to check out the InvokeRepeating command that Eric was talking about, but, in addition to my code to apply what you just said… attach this directly to your object that you want to duplicate and not an empty game object like before.
var frequency : float = 1.0; // Will duplicate every x seconds
private var nextDuplicationTime : float;
private var duplicatesLeft : int = 2;
function Update() {
if (duplicatesLeft) {
if (Time.time > nextDuplicationTime) {
Instantiate(gameObject);
nextDuplicationTime = Time.time + frequency;
duplicatesLeft--;
}
}
}
function Start() {
nextDuplicationTime = Time.time + frequency;
}
And if you never need your object to duplicate again, use this code so that you can save precious CPU power ;)…
Again… I haven’t tested it, but, I think it should work
var frequency : float = 1.0; // Will duplicate every x seconds
private var nextDuplicationTime : float;
private var duplicatesLeft : int = 2;
function Update() {
if (duplicatesLeft) {
if (Time.time > nextDuplicationTime) {
Instantiate(gameObject);
nextDuplicationTime = Time.time + frequency;
duplicatesLeft--;
}
}
else
Destroy(this);
}
function Start() {
nextDuplicationTime = Time.time + frequency;
}
This works wonderfully! Thanks for this, and I’ll be using it wisely and trying not to crash my computer with it’s duplicating power. (It stopped my Unity for about 20 seconds when I set it to duplicate every 0.1 seconds.) Thanks a ton.