- Home /
random name selector
i want a random name selector for the npc where i can have a bunch of names in a list and it randomly picks a name from there and put it as that npc name and can reference that npc from a thread to use in dialogue and and i want it where it cant do this for more then one npc.
ps i have dislexsya sorry for that
Answer by HappyMoo · Jan 14, 2014 at 09:50 PM
using UnityEngine;
using System.Collections;
public class RandomNameTest : MonoBehaviour {
private string[] names = new string[] { "Peter", "Ron", "Satchmo" };
public string GetRandomName()
{
return names[Random.Range(0, names.Length)];
}
void Start()
{
Debug.Log(GetRandomName() + " asks you nicely to use the learning modules");
}
}
Answer by AndyMartin458 · Dec 21, 2013 at 12:48 AM
The unity engine (and C#) have a random function that will grab a random number within a certain range. Here is a quick example for one character. TODO: Make a Names class to store all the names only once, and add a function which will return a name.
void Start()
{
System.Random rand = new System.Random();
int number = rand.Next(0, 500)
List<string> names = new List<string>();
for(int i = 0; i < 500; i++)
{
names.Add(A name that you grab somehow)
}
//now get a random name by using this
string myName = names[number];
}
id not no how to use c# :( so i dont no how to make the name class :( sorry
@Andy$$anonymous$$artin458 sorry but i dont no how to make a name class im sorry my mentor wus teching me c# but wall im a newbe and i wus band frum the irc be cuse i have dislexsya
@cakegamer aww man. I'm sorry about that. I'm sure that you can still learn it. I came across this article about the benefits of program$$anonymous$$g for those with dyslexia. http://io9.com/377561/why-dyslexics-are-good-computer-programmers
Answer by CrazyKane · Jan 15, 2014 at 02:04 AM
You could use the random number generator, then make a switch statement.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Changing Objects Material Using My Array 1 Answer
How should I create a NPC System? 1 Answer
Help with spawning an object in a random location 1 Answer
Choose random background at start 1 Answer