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 Bartman3010 · Mar 05, 2012 at 09:49 PM · arraystringtextmeshfor looplength

Find the length of a string in Javascript

I'm having tremendous difficulty trying to find the length of a string object that can vary as the host String (Not seen in the example) has different Strings split into an array that can be created/expanded in the hierarchy. Every time I try to find the length, it doesn't seem to work. The text object I'm making outputs onto a Textmesh. All the examples I saw of how to get a legnth either dealt with GUIText or finding the width of a GUI object as opposed to being based off of text. I don't want to do either of those. My goal is to print out text in a typewriter fashion, but every time the variable goes to a certain length, the array goes out of range and breaks the script. Is there a way to at least ignore an array once it goes out of range?

Heres what I got down for the typewriter text, at least the best I can interpret from.

var word:String = ("Test.");

var i:int =0;

var hostWord:String;

function Update()

{

     hostWord += word[i];

     i++;               //Not the best way to add text of course, but this shows how quickly a letter can be added.

    Debug.Log(hostWord);  //Can also be seen in the hierarchy, but the debug window shows when it's been updated by every frame according to i++

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DaveA · Mar 05, 2012 at 10:02 PM

word.Length would be that length.

Nowhere do you check for the length of the source, and incrementing i at 60+ frames per second will go over that limit quick.

Nowhere do you check for the length of the destination, I think you are assuming += will auto-expand the string's bounds, right? I'm not sure on that, you may need to allocate as you go, and don't allocate 1 byte at a time, it gets slow quickly. If you are creating long strings this way, pre-allocate the whole biggest chunk, or reallocate in, say, 1k chunks.

Comment
Add comment · Show 4 · 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 Bartman3010 · Mar 14, 2012 at 08:56 PM 0
Share

That's the thing. I'm not sure how to find the length.

I tried several methods.

[code]var strings1$$anonymous$$ax(text: String, charCount : int);[/code] This gives off several errors as if it can't compile the line.

I even tried to use a variable to obtain an amount of characters through these methods: [code]subtitles1Host += dialogue$$anonymous$$ode[npcID].dialogueSetptextString.subtitles1[i]; subtitles1Host += dialogue$$anonymous$$ode[npcID].dialogueSetptextString.subtitles1.Length;[/code]

However these also generate errors as well. I'm really not sure what to do to get variables. I even tried to create a variable named labels as well as Labels, but neither one work. What should I do?

Also as a side note, the string is being accessed through a class.

avatar image DaveA · Mar 14, 2012 at 10:04 PM 0
Share

I think this will help: http://unifycommunity.com/wiki/index.php?title=AutoType

avatar image Eric5h5 · Mar 14, 2012 at 10:38 PM 0
Share

@Bartman3010: DaveA already told you how to get the length: word.Length. Also, [code] tags don't work here, this site uses $$anonymous$$arkDown.

avatar image Bartman3010 · Mar 20, 2012 at 10:08 PM 0
Share

Ah. $$anonymous$$y apologies. Although after trying to use word.Length, it doesn't add up if it's out of the function groups. Putting it inside of the Update function causes the array to be out of range.

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

6 People are following this question.

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

Related Questions

How do I get this string out of this array? 2 Answers

Loop through array until certain value is found. 2 Answers

Label an array of buttons with an array of strings 2 Answers

Can't use remove with string[] ? 1 Answer

Iterating through multidimensional arrays 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