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.
@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.
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.