- Home /
Random selection
Hey guys, i know that this question had already been asked, but i didn't understood what you are supposed to do and what i've tried didn't work really well. I would like to know how do you create a script that basically makes random decisions, selecting one of the available variables. I really wanted to know this, because i can imagine a lot of things i could use, such as: Make AI decide places to go, and things to do while doing other things, randomly spawning things, randomizing the game, switch between scenes, etc...
I want to make something like this:
using UnityEngine;
using System.Collections;
public class TEST : MonoBehaviour {
public GameObject cube;
public GameObject triangle;
public GameObject sphere;
void Update()
{
//Do script things here
this.gameObject.select between cube,triangle or sphere;
//Do something else, such as follow the gameobject of the variable, change something, ...
}
}
Thanks for the help!
Answer by voncarp · Dec 01, 2016 at 07:37 PM
Something like this might be what you are looking for:
using System.Collections;
using UnityEngine;
public class helpscript : MonoBehaviour {
public float timer = 1;
IEnumerator Start() {
for (;;) {
int x = Random.Range (0, 3);
switch (x) {
case 0:
Debug.Log ("cube AI");
break;
case 1:
Debug.Log ("triangle AI");
break;
case 2:
Debug.Log ("sphere AI");
break;
}
yield return new WaitForSeconds (timer);
}
}
}
When it starts every second (timer = 1) it will repeat a random AI choice.