Newbie Question: Clone object

I´m very new to Unity and scripting, and here´s a simple problem I have:

I want to clone a box with a script that I directly use on the box (drag the script on the box):

var prefab : Transform;

for (var i : int = 0;i < 10; i++) {
    Instantiate (prefab, Vector3(i * 2.0, 0, 0), Quaternion.identity);
}

This example takes another object (the prefab) to clone, but I want to clone the box with the script itself.

Is this possible?

Thanks for help!

Try replacing ‘prefab’ with ‘gameObject’.

Doesn´t work

I think NPSF3000 meant :

var prefab : GameObject;

for (var i : int = 0;i < 10; i++) {
Instantiate (prefab, Vector3(i * 2.0, 0, 0), Quaternion.identity);
}

:wink:

This exposes a variable called ‘prefab’
Then I have to manually drag the box into that slot.
But I want the script automatically to do this, since it´s attached to it.

Imagine:
I drag this script onto a box of my scene:
The box instantly gets cloned.
I drag the same script onto a sphere of my scene:
The sphere instantly gets cloned.

No he didn’t. gameObject/this.gameObject is a built-in shortcut to access the current GameObject, so that should work I would think.

for (var i : int = 0;i < 10; i++) {
    Instantiate(this.gameObject, Vector3(i * 2.0, 0, 0), Quaternion.identity);
}

legend 411 is correct.

This is fully functional and tested JS:

I left out the ‘this.’, but if you want you can have it in. I also made the clones positions relative to the Original.

@NPSF3000s: Your script works fine but I actually don´t understand what you´re doing there:

Why are you declaring a variable ‘Original’ that is not used in the function ‘Clone’ at all?
(Why has it to be static, otherwise Unity crashes?)

In my understanding the object that is instantiatet here (gameObject) is not defined at all!?
Or does ‘gameObject’ automatically refer to the Gameobject the script is used on?

We are writing in a ‘monobehaviour’. Though you can’t see it, there is a var called ‘GameObject’ and it refers to, you guessed it, whatever gameObject you are on.

Now there are two parts to my script, the clone, and the Start Function that calls the clone. Here is an explanation of the latter.

All I do in the start function, is prevent clone from being called again - otherwise every single time I clone an object, i’ll clone 10 more, which will in return clone 10 more each… and never stop.

Slightly better version - not tested.

http://pastebin.com/2N4WmgT9

Should be a tad easier to follow.

A static var is the same for ever instance of the class, so if it is set to true in one instance all other instances (e.g. the clones) see it as what it was set to. In this simple case, it is false once (allowing the clone) and forever after false (preventing more cloning and infinite loops).

Now the only reason I do any of this is because Start is called on every clone as soon as it starts, if you call clone in, say, OnMouseDown, then it becomes unnecessary.

I´ve edited the code so that I understand what happens and it works fine, too:
See that I instanciate ‘Original’.

Thanks very much!