Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to spawn game objects in a specific area repeatedly?

Discussion in 'Scripting' started by MichaelHotte, Feb 26, 2015.

  1. MichaelHotte

    MichaelHotte

    Joined:
    Feb 22, 2015
    Posts:
    31
    Okay my question is pretty obvious. I want to spawn game objects in a selected area of the game more than once. My code below allows me to spawn game objects in one spot, but it does it continuously and never stopping. Thus my problem is it creates too many at once which will eventually start to freeze up the unity software because of all the objects created. I'd like to have the objects created within a random time selection (like within 1 to 5 seconds a piece) along with the objects created in a selected area. (such as on the x axis in between two numbers like the time frame above. My code I am currently stuck with is below. I have searched multiple forums and videos with hopes of an answer, but several of them don't work or is not what i'm looking for. I realize this may be a lot to ask for, or i'm just dumb but either way ANYTHING will help. Suggestions, answers, examples, maybe your own versions of code, etc. It doesn't matter where or what I just really really need the help. Thanks in advance.

    #pragma strict

    var cube : GameObject;
    var spawn_point;
    var timer = 0.0;

    function spawn_cube ()
    {
    var spawn_position = Vector3(0,0,0);
    var temp_spawn_cube = Instantiate(cube, spawn_position, Quaternion.identity);
    }

    function Start () {

    spawn_cube();

    }

    function Update () {

    }

    The timer is there because i've tried using it before, but have failed with success.
     
  2. XenoJester

    XenoJester

    Joined:
    Feb 25, 2015
    Posts:
    13
  3. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    Seeing your code, that will allow you to instantiate just once....

    you can implement a timer or use directly yield WaitForSeconds to idle for a while..
    but a timer is better

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var cube : GameObject;
    5. var spawn_point;
    6. var changeTimer : boolean = true;
    7. var canInstantiate : boolean = false;
    8.  
    9. function Update ()
    10. {
    11.  
    12. if (changeTimer)
    13. {
    14.     changeTimer = false;
    15.     var timer = Random.Range (1f, 5f);
    16. }
    17.  
    18. timer -= Timer.deltaTime;
    19.  
    20. if (timer < 0.0f)
    21. {
    22.      timer = 0;
    23.      canInstantiate = true;
    24. }
    25.  
    26. else canInstantiate = false;
    27.  
    28. if (canInstantiate)
    29. {
    30.     var spawn_position = Vector3(0,0,0);
    31.     var temp_spawn_cube = Instantiate(cube, spawn_position, Quaternion.identity);
    32.     changeTimer = true;
    33. }
    34.  
    35.  
     
    Last edited: Feb 26, 2015
  4. MichaelHotte

    MichaelHotte

    Joined:
    Feb 22, 2015
    Posts:
    31
    Assets/Spawner.js(12,21): BCE0019: 'deltaTime' is not a member of 'float'.

    Assets/Spawner.js(13,6): BCE0005: Unknown identifier: 'canInstantiate'.

    I tried your code and got these errors. Also you think my code would only create it once but it creates it until it is unable to make more of those objects.
     
  5. MichaelHotte

    MichaelHotte

    Joined:
    Feb 22, 2015
    Posts:
    31
    Assets/Spawner.js(5,19): BCE0018: The name 'bool' does not denote a valid type ('not found').

    I tried again with that code and this came up. I would try to fix this error but still learning and not quite sure how to fix this.
     
  6. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    oops.. i was in an error..
    in line 17
    replace this:
    Code (csharp):
    1.  
    2. Timer.deltaTime;
    3.  
    with this:
    Code (csharp):
    1.  
    2. Time.deltaTime;
    3.  
    Regarding to the "bool" problem.. ..
    i'm not using UnityScript.. maybe you want to change it to "boolean"..
     
  7. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    Go with this updated one:
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var cube : GameObject;
    5. var spawn_point;
    6. var changeTimer : boolean = true;
    7. var canInstantiate : boolean = false;
    8.  
    9. function Update ()
    10. {
    11.    if (changeTimer)
    12.    {
    13.        changeTimer = false;
    14.       var timer = Random.Range (1f, 5f);
    15.    }
    16.  
    17.    timer -= Timer.deltaTime;
    18.  
    19.  
    20.    if (timer < 0.0f)
    21.    {
    22.         timer = 0;
    23.         canInstantiate = true;
    24.    }
    25.  
    26.    else canInstantiate = false;
    27.  
    28.    if (canInstantiate)
    29.    {
    30.       var spawn_position = Vector3(0,0,0);
    31.       var temp_spawn_cube = Instantiate(cube, spawn_position, Quaternion.identity);
    32.        changeTimer = true;
    33.    }
    34. }
    35.  
     
  8. MichaelHotte

    MichaelHotte

    Joined:
    Feb 22, 2015
    Posts:
    31
    I went with this code which worked with creating the objects, but it'll create way too many and cause the software to lag and freeze up like before until unity says it is unable to create anymore.
     
  9. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Of course it will. You need some logic to limit how many objects can be spawned, something like a counter and only spawn objects when there are less than that amount.
     
  10. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    Yes, Michel, a counter like Korno said, is the way to restrict the quantity of objects..
    remember, you can destroy the instantiated object after a couple of seconds..
    with Destroy(temp_spawn_cube, 3f) where 3f is 3 seconds..
     
  11. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    This updated script may help you to restrict elements to 10..
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var cube : GameObject;
    5. var spawn_point;
    6. var changeTimer : boolean = true;
    7. var canInstantiate : boolean = false;
    8. var counter : int = 10;
    9.  
    10. function Update ()
    11. {
    12.    if (changeTimer)
    13.    {
    14.        changeTimer = false;
    15.       var timer = Random.Range (1f, 5f);
    16.    }
    17.  
    18.    timer -= Timer.deltaTime;
    19.  
    20.  
    21.    if (timer < 0.0f)
    22.    {
    23.         timer = 0;
    24.         if (counter >= 0) canInstantiate = true;
    25.    }
    26.  
    27.    else canInstantiate = false;
    28.  
    29.    if (canInstantiate)
    30.    {
    31.       counter--;
    32.       var spawn_position = Vector3(0,0,0);
    33.       var temp_spawn_cube = Instantiate(cube, spawn_position, Quaternion.identity);
    34.       changeTimer = true;
    35.    }
    36. }
    37.  
     
    MichaelHotte likes this.
  12. MichaelHotte

    MichaelHotte

    Joined:
    Feb 22, 2015
    Posts:
    31
    Thanks for all the help you've given! I'm still learning and appreciate it! Also the code was tested again with the counter and works just fine!
     
  13. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    great... enjoy my friend.. :)