# How to assign random positions to gameobjects from an array?

Hi there! I really need your help!!

This is what I’m trying to do:

When my game Start, appear several gameobjects. After a few seconds, each gameobject need to be re-located in a new and random position. An array need to contains those positions. For example, array[0] = transform.position = Vector3(x,y,z);

How can I create an array that contains positions of gameobjects? and
How can I assign those positions to each gameobject randomly?

I apologize if my questions is dumb.

Here’s a short example that returns a random position from a preset array of Vector3. If the position is already in use, it’ll attempt to return a free one a few times, if it cannot find any, it’ll return positions[0].

``````var positions:Vector3[];
var inUse:boolean[];
var maxAttempt:int = 10;

function GetPosition():Vector3
{
var attempt:int = 0;
var index:int = 0;

do
{
index = Mathf.Random.Range(0,positions.Length);
}
while(inUse[index] && ++attempt < maxAttempt);

inUse[index] = true;
return positions[index];
}
``````

Unless you want to generate the position randomly from a base position?

``````function GetRandomPosition(basePosition:Vector3,radius:float):Vector3
{
}
``````

BY0LOG1C, Thanks so much for you answer!

I’m still wondering, how can I set the array with positions (x,y,z)?

I’m going to explain you exactly what I need, please I apologize because of my English!

When my game Load, a set of “books” (cubes) appears very near from the Camera, and after 10 seconds, each book is going to be re-located in a sideboard/shelf which is further than the books.

What I need is to create an Array that contains those positions:
Example: array[0] = transform.position = Vector3(x,y,z)

And then, how can I assign to my gameobjects a random position (which are contain in the array)? Do I need a Script for each gameobject?

I’m not good at Scripting!

Thanks so much again!