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 TimNick151297 · May 21, 2016 at 06:08 PM · randomstringchar

Replace one random char in a string

Hi there,

I'm currently working on a script for changing a random char in a string. Here's what I've got so far:

 import System.Text;
 #pragma strict
 
 var speed : float = 0.5;
 var text : UI.Text;
 var chars : int = 1500;
 var oldText : StringBuilder;
 
 
 function Start () {
     
     oldText = new System.Text.StringBuilder(text.text);
     Change();
    
 }
 
 function Change () {
                  
     while(true) {
        
        oldText[Random.Range(0, chars-1)] = Random.Range(0, 2); 
        text.text = oldText.ToString();
        yield WaitForSeconds(speed);
         
     }
 
 }
 
 

When I hit play, the text just vanishes in steps. I don't know, what I'm doing wrong.

Thanks for your response!

Comment
Add comment · Show 1
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 TimNick151297 · May 22, 2016 at 02:07 PM 0
Share

I figured it out. If anyone is interested in the code, here you go:

 import System.Text;
 #pragma strict
 
 var speed : float = 0.5;
 var text : UI.Text;
 var chars : int;
 var oldText : StringBuilder;
 
 function Start () {
 
     oldText = new System.Text.StringBuilder(text.text);
     chars = oldText.Length;
     Change();
    
 }
 
 function Change () {
      
     var numChars = ['0', '1']; 
 
     while(true) {
        
         var random = Random.Range(0, chars-1);
         oldText.Chars[random] = numChars[Random.Range(0, 2)][0];
         text.text = oldText.ToString();
         yield WaitForSeconds(speed);
                
     }
 
 }
 

Thanks to Ahndrakhul for the help!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ahndrakhul · May 21, 2016 at 10:36 PM

Are you attempting to replace characters in the stringbuilder with random 1's and 0's? I think that in C# you would get an error trying to do this, but maybe javascript automatically casts ints to chars. What's probably happening is that your characters are being replaced by whatever characters correspond to 0 and 1 in ascii or unicode code. I think 0 is null.

could you try using the random number to pick a character out of an array?

 function Change () 
 {
      var numChars = ['0', '1'];             
      while(true) 
      {       
         oldText[Random.Range(0, chars-1)] =  numChars[Random.Range(0, 2)]; 
         text.text = oldText.ToString();
         yield WaitForSeconds(speed);
          
      }
 }

Sorry if the array isn't declared correctly. I really don't know javascript well at all.

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 TimNick151297 · May 22, 2016 at 11:29 AM 0
Share

First of all, thanks for your answer! Now I get the following errors: alt text

unbenannt.png (35.4 kB)

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

56 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

Related Questions

Wher is the problem? My string isn't behaving... 1 Answer

How to make a random string from array in C#? 1 Answer

Pick a random string from a string array C# 1 Answer

Randomly Changing Strings? 1 Answer

Check if 2 InputField contains 2 words (not the same)? Feel like it's easy but can't figure out what's wrong 0 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