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
1
Question by zerox911 · May 24, 2012 at 06:21 AM · arraystringsplitwordindividual

how can i split a word to individual letters..??

hi all.

i need some insights on how to accomplish this...

i already have an array of words.. and it is shown in the scene as GUI.Label...

once its being played.. words from the array is shown...

now.. i need some codes/tips/ideas/etc to do this..

i want the words from the array appear in the scene as individual letters...

so it'll be like...

ARRAY : BALL, CHICKEN ....

in the GUI SCENE : B . A . L . L ... C . H . I . C . K . E . N

any kind of help is appreciated...

thanks for your honorable a attention..

Comment
Add comment · Show 2
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 David C · May 24, 2012 at 06:25 AM 0
Share

Do you want the letters as separate GUI labels?

avatar image zerox911 · May 24, 2012 at 07:02 AM 0
Share

yep... as GUI labels..

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by flamy · May 24, 2012 at 07:04 AM

  //incase it is string array.  /*declaration*/
     
     var length:int=10;
     var names:String[];    // some values added to the array from editor, you can add it in code too ur wish
     
     
     var x=10;
     var y=100;
     var width=30;
     var height=30;
     function Start()
     {
       
     }
     
     function OnGUI()
     {
 
        if(Event.current.type==EventType.Repaint)
       {
         // to display the seperate  letters.
         for(var i=0;i<names.Length;i++)
         {
            for(var j=0;j<names[i].Length;j++)
             GUI.Label(Rect(x+j*width,y+i*height,width,height/*x*//*y*//*width*//*height*/),""+names[i][j]);
        }
     
      }
   

   }


Update: changed as per @Bunny83 's suggestion

Comment
Add comment · Show 14 · 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 zerox911 · May 24, 2012 at 07:10 AM 0
Share

what does

THIS :

for(var j=0;j x// y// width// height/),""+names[i].ToCharArray()[j]); }

...do???

avatar image flamy · May 24, 2012 at 07:25 AM 0
Share

Rect(x+j*width,y+i*height,width,height)

the rect doesnt 2 things, first it prints individual letters with certain gap in x and also moves the control to the new line when the new word starts, width is the gap in horizontal axis(including the space occupied by the letter) and height is the gap in vertical axis. x and y the starting positions.

names[i].ToCharArray()[j] separates individual letters.

for(var j=0;j

avatar image zerox911 · May 24, 2012 at 07:32 AM 0
Share

owh...

thanks .. i get it now...

avatar image flamy · May 24, 2012 at 07:42 AM 0
Share

is this what you was looking for??!

avatar image Bunny83 · May 24, 2012 at 05:01 PM 2
Share

Just want to add that the string class has an indexer so you can easily access single characters from a string.

 for(var i = 0; i < names.Length; i++)
 {
     for(var j = 0; j < names[i].Length; j++)
     {
         GUI.Label(Rect(x + j*width, y + i*height, width, height), "" + names[i][j]);
     }
 }

This would save you alot overhead since you convert the whole string into a char array for each character. That means since OnGUI is called 2 times per frame for a 10 character word you convert this word 40 times into an array...

Show more comments
avatar image
0

Answer by lyzard · May 24, 2012 at 06:33 AM

given a string str, you can access each character like an array e.g. str[i]. Maybe you can start with something like:

string test = "chicken";

string output = "";

for(int i=0; i<test.Length; i++)

output+=test[i]+".";

Debug.Log(output);

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 zerox911 · May 24, 2012 at 07:04 AM 0
Share

a brief explanation about your snippet code, please... thanks..

avatar image lyzard · May 24, 2012 at 09:54 AM 0
Share

this snippet simply builds a string called output by iterating over each character of the string called test. The loop starts at index 0 up to index given by the length of the test string. At each iteration, output+=test[i]+"." adds the character of string test at index i and "." to the output string. It is equivalent to output=output+test[i]+"." So as variable i gets incremented, it adds first test[0]+"." (first character of test string followed by "."), test[1]+"." (second character of test string followed by ".") and so on. At the end, it outputs the built string. Hope this helps ;)

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Splitting String only on a "space". Using my method --- FIXED 2 Answers

convert string to list of lists 1 Answer

How to convert a string to int array in Unity C# 1 Answer

Change part of a string [Solved] 3 Answers

Strings, Arrays and Split in js 1 Answer


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