# spawning object at random defined points

**URL:** https://discussions.unity.com/t/spawning-object-at-random-defined-points/580415
**Category:** Scripting
**Tags:** Scripting
**Created:** [May 13, 2015, 9:06am UTC](https://discussions.unity.com/t/spawning-object-at-random-defined-points/580415 "2015-05-13T09:06:58Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ghost123\_1234](https://avatars.discourse-cdn.com/v4/letter/g/8dc957/32.png) [@ghost123\_1234](https://discussions.unity.com/u/ghost123_1234)
#### Post date: [May 13, 2015, 9:06am UTC](https://discussions.unity.com/t/spawning-object-at-random-defined-points/580415/1 "2015-05-13T09:06:58Z")

</div>

hello

I want that an object is instantiated when i press Q  
right now i have this for that

```csharp
using UnityEngine;
using System.Collections;

public class kleidoufschieten : MonoBehaviour {
   
    public Rigidbody projectile;

    // Use this for initialization
    void Start ()
    {
    }
   
    // Update is called once per frame
    void Update ()
    {
        if(Input.GetKeyDown(KeyCode.Q))
         {
            Vector3 euler = transform.eulerAngles;
            euler.x = 330 + Random.Range(0, 3) * 5;
            transform.eulerAngles = euler;
            Rigidbody clone;
            clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
            clone.velocity = transform.TransformDirection(Vector3.forward * 24);

        }
    }
}

```

this script is on the emty gameobject where the projectile shoots from. When i press Q it first rotates the object on the x axes and then shoots it.  
But now i want to add another empty gameobject where it can shoot from. so when i press Q it picks an object random and then shoots that from it. So for example one empty gameobject is from the left and one is from the right.  
That when i press Q it needs to sometimes do left right right left etc.  
but it also needs to give it a rotatoin so the:

```csharp
Vector3 euler = transform.eulerAngles;
            euler.x = 330 + Random.Range(0, 3) * 5;

```

but how do i do that?  
hope someone can help

---

<div class="post-metadata">

### Author: ![LeftyRighty](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@LeftyRighty](https://discussions.unity.com/u/LeftyRighty)
#### Post date: [May 13, 2015, 9:41am UTC](https://discussions.unity.com/t/spawning-object-at-random-defined-points/580415/2 "2015-05-13T09:41:03Z")

</div>

put the script on a parent empty gameobject, the “manager” of this behaviour. Add children to the parent which represent the spawn points to use in the scene.

In Start() do

```auto
... = GetComponentsInChildren<Transform>();

```

(or if you want create a blank class script for the spawn points and search for that type rather than Transform 🙂 )  
to pick up all the points and put it into an array/list/whatever collection. Then look up Random.Range and use that to pick a random spawn in the collection.

---

<div class="post-metadata">

### Author: ![ghost123\_1234](https://avatars.discourse-cdn.com/v4/letter/g/8dc957/32.png) [@ghost123\_1234](https://discussions.unity.com/u/ghost123_1234)
#### Post date: [May 13, 2015, 11:41am UTC](https://discussions.unity.com/t/spawning-object-at-random-defined-points/580415/3 "2015-05-13T11:41:40Z")

</div>

yeah i understand this but i dont really now how to do it in code?

---

<div class="post-metadata">

### Author: ![LeftyRighty](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@LeftyRighty](https://discussions.unity.com/u/LeftyRighty)
#### Post date: [May 13, 2015, 1:00pm UTC](https://discussions.unity.com/t/spawning-object-at-random-defined-points/580415/4 "2015-05-13T13:00:59Z")

</div>

```auto
void Start()
{
    Transform[] spawnPoints = GetComponentsInChildren<Transform>();
}

void Update()
{
    if(Input.GetKeyDown(KeyCode.Q)))
    {
        chosenSpawnPoint = spawnPoints[Random.Range(0, spawnPoints.Length - 1)];
     
        // rest of code
        // use "chosenSpawnPoint.position" rather than "transform.position"
        // use "chosenSpawnPoint.rotation" rather than "transform.rotation"
    }
}

```

(not checked for errors 😛 )

when you put the empty game objects into the scene make sure they are rotated correctly so when you use their rotation they go left/right as you want

---

<div class="post-metadata">

### Author: ![ghost123\_1234](https://avatars.discourse-cdn.com/v4/letter/g/8dc957/32.png) [@ghost123\_1234](https://discussions.unity.com/u/ghost123_1234)
#### Post date: [May 14, 2015, 9:47am UTC](https://discussions.unity.com/t/spawning-object-at-random-defined-points/580415/5 "2015-05-14T09:47:53Z")

</div>

that worked thanks
