Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Bitpocketer · Jun 30, 2016 at 07:24 PM · c#scripting problemui

How do i change a color of an individual alphabet of a UI.Text

Hello devs Good Evening/Morning. I want to change a color of an individual alphabet of UI Text from scrip using tags of rich color, following is the snippet code I'm trying to get it done with void OnCollisionEnter2D(Collision2D col) { if (col.gameObject.tag == "Player") { // Destroy(col.gameObject); char cubealphabet=gameObject.GetComponent<AlphabetAssigner>().alphabet.text[0]; Debug.Log(cubealphabet); string currentword = GameObject.Find("GameController").GetComponent<SpawningPlatforms>().currentword; //Debug.Log("current word from destroyer"+GameObject.Find("GameController").GetComponent<SpawningPlatforms>().currentword); if (GameObject.Find("GameController").GetComponent<SpawningPlatforms>().wordbank.checkalphabet(currentword, cubealphabet) == true) { Debug.Log("accurate alphabet"); GameObject.Find("GameController").GetComponent<SpawningPlatforms>().highlightalphabet(cubealphabet); } else { Debug.Log("Incorrect alphabet"); } Destroy(gameObject); } } the variable cubealphabet just takes a single letter from the gameobject it is on,and it is sent to highlightalphabet function to change its color, following is the snippet code of highlighalphabet function public void highlightalphabet(char colalpha) {

         int alphabetindex = wordfield.text.IndexOf(colalpha);
         Debug.Log("Alphabet index is " + alphabetindex);
         if (alphabetindex == -1)
             return;
         char coloredalpha=wordfield.text[alphabetindex];
         //wordfield.text = "";
         for (int i = 0; i <= wordfield.text.Length - 1; i++)
         {
             //wordfield.text +=currentword[i];
             if(i==alphabetindex)
             wordfield.text+= "<color=#FF0000>" + coloredalpha + "</color>";
         }
        
     }

the problem here is , I can't seem to access an individual character in text with it's respective index like this wordfield.text[i]=""+coloredalpha+""; the error i face here is "can not implicitly convert type string to char" pardon if the question isn't elaborated enough, but the bottom line is, I can't apply richtext properties to an individual alphabets of UI.text.

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
0
Best Answer

Answer by Ahndrakhul · Jun 30, 2016 at 08:40 PM

You can use StringBuilder for this sort of thing. You would need to add using System.Text; to your script and then do something like this:

 public void highlightalphabet(char colalpha)
 {
     StringBuilder strBuilder = new StringBuilder(wordfield.text);
     strBuilder.Replace(colalpha.ToString(), "<color=#FF0000>" + colalpha + "</color>");
     wordfield.text = strBuilder.ToString();
 }

This is a really simple example that changes the color of all characters in a string that match the "colalpha" char. It gets more difficult if you want to do things like remove the color change, or change the color of previously altered text, but it's not too hard.

Comment
Add comment · 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
0

Answer by Bitpocketer · Jul 01, 2016 at 07:07 PM

@Ahndrakhul thank you very much, it did a great job but the problem with it is, it replaces all the occurrences of specified character, my scenario is, let there be a word "Moon", alphabets are floating and user hits 'o' both of those o's are highlighted where as i want just one highlighted o for a single hit, another 'o' has to be highlighted when user hits the o for second time.

Comment
Add comment · Show 3 · 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 Ahndrakhul · Jul 01, 2016 at 07:52 PM 0
Share

I've just realized how bad my original answer is. It will totally fail in the case of your moon example if you hit the m or the n first and then hit the o. It will try to change the color of the o's in the richtext tags. Do you want the o's to be highlighted from left to right, or do you want it to be random?

avatar image Ahndrakhul · Jul 02, 2016 at 10:25 AM 0
Share

Try this. It's not great, but it should work with your current setup. It will fail if you try to highlight the "less than" character.

 public void highlightalphabet(char colalpha)
     {       
         string word = wordfield.text;
         for (int i = 0; i < word.Length; i++)
         {
             if (word[i] == '<')
             {
                 while (i + 1 < word.Length && word[++i] != '>') { }
             }
             if (word[i] == colalpha)
             {
                 if (i >= word.Length - 2 || word.Substring(i + 1, 2) != "</")
                 {
                     word = word.Remove(i, 1);
                     word = word.Insert(i, "<color=#FF0000>" + colalpha + "</color>");
                     break;
                 }
             }
         }
         wordfield.text = word;
     }
avatar image Bitpocketer Ahndrakhul · Jul 04, 2016 at 07:15 PM 0
Share

Thank you very much, Perfect, It worked exactly the way i wanted it to work, How do i figure out when entire word has been highlighted?

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

187 People are following this question.

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

Related Questions

Certain scripts that are attached does not work after build, scene change and closing unity 0 Answers

How to send event to my canvas via script 0 Answers

Always pickup closest item? 0 Answers

I get a "NullReferenceException" when trying to change the text of a UI text box. 1 Answer

Null Reference Exception only in second method 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