Search Unity

Spawning game objects in selected areas?

Discussion in 'Physics' 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. samba

    samba

    Joined:
    Feb 25, 2015
    Posts:
    7
    That's better if you take some random positions where you want to re-spawn .