- Home /
Random Text String
Hi, I'm having trouble figuring out a way to randomise the text of a 3d text object. I'm aiming for having a selection of 5 different strings for the computer to choose from. I'm then thinking that having a random float calculate between 0 and 5.
I can't figure out how to have the number that the float randomly chooses be assigned to the 5 different string variables.
Basically, anyone want to add to this script to see if they can figure out how to select a random string and assign it the the 3d text game object (npcChitChat var)
Thanks in advance
//random text variables
Private var PrefabNum : float =0; var textLine1 = "hello world"; var textLine2 = ""; var textLine3 : String = "three"; var textLine4 : String = ""; var textLine5 : String = "";
function OnTriggerEnter (myTrigger : Collider) {
//checks to see if the players character is in the trigger area. If so, then it starts the trigger if(myTrigger.gameObject.name == "Player") { //prevent the script from firing the text multiple times, this checks whether the timer has begun. if(npcChitChat.active == false) {
//random text generation
//displays the text
Debug.Log("NPC is Chit Chatting");
npcChitChat.GetComponent.<TextMesh>().text = textLine1;
npcChitChat.active = true;
//changes camera
if(cameraZoom == true)
{
Debug.Log("camera wants to zoom");
cameraObject.active = true;
}
//invoke the text so that it disappears after the var timeToWait
Invoke("StartChitChat", timeToWait);
}
}
}
function StartChitChat () {
Debug.Log("NPC is Chit Chatting");
npcChitChat.active = false;
if(cameraZoom == true)
{
Debug.Log("stopped the camera zoom");
cameraObject.active = false;
}
}
Answer by Alec-Slayden · Feb 24, 2011 at 04:05 PM
You might want to use a string array instead of separate variables, or composed of the contents of those variables.
you can initialize it like so (Javascript example):
var myLines = new String[5]; //string array with 5 strings
function Start(){ myLines[0] = "Hello World"; myLines[1] = ""; myLines[2] = "three"; myLines[3] = ""; myLines[4] = ""; }
from there it's just a matter of a random int between 0 and 4, which you use with
npcChitChat.GetComponent.<TextMesh>().text = myLines[randomInt];
Cheers Alec, this seems like a much more optimised version of the code I had created (using alot of if statements)
glad it can be of use! You'll still need to generate your own random number but that should be simple enough.
Answer by Greg 9 · Feb 27, 2011 at 11:52 AM
Alec's above comment looks to be a much more optimised version of the fix I had come up with.
Basically, I'm using if statements to pic from a series of 5 variable strings:
//random text generation
var chance =(Random.Range(1, 2));
//displays the text
if(chance == 1)
{
Debug.Log("NPC is Chit Chatting");
npcChitChat.GetComponent.<TextMesh>().text = textLine1;
npcChitChat.active = true;
}
if(chance == 2)
{
Debug.Log("NPC is Chit Chatting");
npcChitChat.GetComponent.<TextMesh>().text = textLine2;
npcChitChat.active = true;
}
The original script had a Random.Range from 1 through to 5. But I'm going to try implementing the one Alec suggested.
When you find that you have to use a chain of if statements for a certain variable's settings, you may want to use the Switch() statement ins$$anonymous$$d. In addition to saving some typing, it will allow a couple interesting conditional options. http://www.w3schools.com/js/js_switch.asp