Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Greg 9 · Feb 24, 2011 at 03:04 PM · textrandom

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;
     }

 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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];
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Greg 9 · Feb 27, 2011 at 11:49 AM 0
Share

Cheers Alec, this seems like a much more optimised version of the code I had created (using alot of if statements)

avatar image Alec-Slayden · Feb 27, 2011 at 06:20 PM 0
Share

glad it can be of use! You'll still need to generate your own random number but that should be simple enough.

avatar image
0

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.&lt;TextMesh&gt;().text = textLine1;
 npcChitChat.active = true;
 }

     if(chance == 2)
 {
 Debug.Log("NPC is Chit Chatting");
 npcChitChat.GetComponent.&lt;TextMesh&gt;().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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Alec-Slayden · Feb 27, 2011 at 06:23 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

To see chosen random text UI Unity,To Access UI text 0 Answers

How would I show the value of a random value? 1 Answer

How to generate prefab with random number inside it ? 1 Answer

All my text has become just random numbers 1 Answer

How can I match and display two random numbers using C#? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges