# Arrays -

I have the following code which moves an object to 1 of 5 different positions by choosing which one to move at by random. My question is how can we put these 5 Way Points in an array or a list. What I need to is add 50 WayPoints to a list with out using 50 if Statements. Any help would be greatly appreciated?

using UnityEngine; using System.Collections;

public class Fleeing : MonoBehaviour {

``````public Transform objectToFleeFrom;
public Transform WayPoint;
Vector3 chaseVel, chaseDir, chasPos;
bool flag = false;
Vector3[] positions;
private int i;
private int rnd1;
void Start()
{
chaseVel = new Vector3(3f, 3f, 3f);
i = Random.Range(1,5);
}

void Update()
{

if (i==1)
{
WayPoint.position = GameObject.Find("WayPoint00").transform.position;
}
if (i==2)
{
WayPoint.position = GameObject.Find("WayPoint01").transform.position;
}
if (i==3)
{
WayPoint.position = GameObject.Find("WayPoint02").transform.position;
}
if (i==4)
{
WayPoint.position = GameObject.Find("WayPoint03").transform.position;
}
if (i==5)
{
WayPoint.position = GameObject.Find("WayPoint04").transform.position;
}

chaseDir = WayPoint.position - transform.position;
Vector3 tmpNormalizedDir = Vector3.Normalize(chaseDir);
chaseDir = new Vector3(tmpNormalizedDir.x * chaseVel.x, tmpNormalizedDir.y * chaseVel.y, tmpNormalizedDir.z * chaseVel.z);
chaseDir *= Time.deltaTime*50;

if((objectToFleeFrom.position - this.transform.position).sqrMagnitude < 400000.0f)
{
flag = true;
}

if(flag==true)
{

this.transform.position += chaseDir;

float x_ = WayPoint.transform.position.x-transform.position.x;
float y_ = WayPoint.transform.position.y-transform.position.y;
float z_ = WayPoint.transform.position.z-transform.position.z;

if(x_<2 && x_>-2 && y_<2 && y_>-2 && z_<2 && z_>-2)
{
flag = false;
int g = Generate();
while(g==i)
{
g = Generate();
}
i = g;

print(i);
}
}
}

int Generate()
{
rnd1 = Random.Range(1,5);
return rnd1;
}

``````

}

maybe what you can do is something like this:

``````private int i;
void Start()
{
chaseVel = new Vector3(3f, 3f, 3f);
i = Random.Range(1,50);
}

void Update(){

if (i<=9){
WayPoint.position = GameObject.Find("WayPoint0"+i).transform.position;
}else{
if(i>=10){
WayPoint.position = GameObject.Find("WayPoint"+i).transform.position;
}
}

``````