Printing a random value from an array.
Hi! I'm going to develop a static name picker (that means I'm not going to use any database because I DONT KNOW IF THAT'S POSSIBLE IN UNITY). Anyways, I don't know how to print a random value from an array that I have declared. Does anybody know how to?
This is my code.
using UnityEngine;
using System.Collections;
public class Names : MonoBehaviour {
void Start()
{
string[] names = new string[3];
names[0] = "Jon";
names[1] = "Jen";
names[2] = "Jun";
//I need to randomly pick and print a value from this point.
print(names[0]);
}
}
I'm still testing stuff out so once I get it working on the console, I'll implement them on a UI.
If someone has any better ideas, that would be great.
Answer by Jordi-Bonastre · May 04, 2016 at 02:39 PM
Use Random.Range to select the index: http://docs.unity3d.com/ScriptReference/Random.Range.html
I'm not going to use any database because I DONT KNOW IF THAT'S POSSIBLE IN UNITY
Why don't use JSON? http://docs.unity3d.com/Manual/JSONSerialization.html
I don't know how to, yet, at least. Can JSON act as a database and store multiple rows of string data from a text box and make them retrievable so that I can randomly pick a value from the result set, just like a database? I don't know how to tho.
Random.Range works great btw. Thanks bruh
You can read the strings using JSON and store them into a string array.