Ramdomly select a variable
I am making a game which involves combat, and i wanted to make my enemy wander around. The best option that i have found was to store some empty gameobjects on some variables and make my enemy choose between them and follow one, but i don't know how to ramdomize it. How can i make my enemy choose between different variables? Something like this:
public GameObject location1;
public GameObject location2;
public GameObject location3;
public GameObject location4;
public GameObject location5;
public GameObject location6;
void Start()
{
StartCoroutine(FollowCoordinates());
}
void Update()
{
}
//This will set the target position for the NavmeshAgent from time to time.
IEnumerator FollowCoordinates()
{
while (true)
{
//This will set the target coordinates
GetComponent<NavMeshAgent>().SetDestination(?????.position); // The "?????" is where i want to make the random choice.
//Wait one second and then loop
yield return new WaitForSeconds(1);
}
}
}
Sorry for the giant amount of variables, i am new to C-Sharp.
Sorry, was replying with a "correct" answer, but misread your code. Please don't hold it against me, haha.
Answer by brunocoimbra · Nov 22, 2016 at 12:05 AM
Instead of using that amount of variables, use arrays:
public GameObject[] locations;
void Start()
{
StartCoroutine(FollowCoordinates());
}
IEnumerator FollowCoordinates()
{
while (true)
{
GetComponent<NavMeshAgent>().SetDestination(locations[Random.Range(0, locations.length)].transform.position);
yield return new WaitForSeconds(1);
}
}
}
I am getting an error: " EnemyWander.cs (18,57): error CS0178: Invalid Rank specifier: expected ',' or ']' "
Sorry, edited my post with the missing character...