Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Instantiate question

Discussion in 'Scripting' started by boomaster_legacy, Sep 3, 2008.

  1. boomaster_legacy

    boomaster_legacy

    Joined:
    Feb 24, 2008
    Posts:
    262
    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.
     
  2. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    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 ***

    Code (csharp):
    1.  
    2. var duplicationObject : GameObject;
    3. var frequency : float = 1.0; // Will duplicate every x seconds
    4.  
    5. private var nextDuplicationTime : float;
    6.  
    7. function Update() {
    8.      if (Time.time > nextDuplicationTime) {
    9.           Instantiate(duplicationObject);
    10.           nextDuplicationTime = Time.time + frequency;
    11.      }
    12. }
    13.  
    14.  
    15. function Start() {
    16.      nextDuplicationTime = Time.time + frequency;
    17. }
    18.  
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Probably simpler just to use InvokeRepeating.

    --Eric
     
  4. boomaster_legacy

    boomaster_legacy

    Joined:
    Feb 24, 2008
    Posts:
    262
    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.
     
  5. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    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.

    Code (csharp):
    1.  
    2. var frequency : float = 1.0; // Will duplicate every x seconds
    3.  
    4. private var nextDuplicationTime : float;
    5. private var duplicatesLeft : int = 2;
    6.  
    7. function Update() {
    8.      if (duplicatesLeft) {
    9.           if (Time.time > nextDuplicationTime) {
    10.                Instantiate(gameObject);
    11.                nextDuplicationTime = Time.time + frequency;
    12.                duplicatesLeft--;
    13.           }
    14.      }
    15. }
    16.  
    17.  
    18. function Start() {
    19.      nextDuplicationTime = Time.time + frequency;
    20. }
     
  6. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    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 :)

    Code (csharp):
    1.  
    2. var frequency : float = 1.0; // Will duplicate every x seconds
    3.  
    4. private var nextDuplicationTime : float;
    5. private var duplicatesLeft : int = 2;
    6.  
    7. function Update() {
    8.      if (duplicatesLeft) {
    9.           if (Time.time > nextDuplicationTime) {
    10.                Instantiate(gameObject);
    11.                nextDuplicationTime = Time.time + frequency;
    12.                duplicatesLeft--;
    13.           }
    14.      }
    15.      else
    16.           Destroy(this);
    17. }
    18.  
    19.  
    20. function Start() {
    21.      nextDuplicationTime = Time.time + frequency;
    22. }
    23.  
     
  7. boomaster_legacy

    boomaster_legacy

    Joined:
    Feb 24, 2008
    Posts:
    262
    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.