# JavaScript WordWrap for 3D text

**URL:** https://discussions.unity.com/t/javascript-wordwrap-for-3d-text/401658
**Category:** Scripting
**Tags:** Scripting
**Created:** [November 14, 2009, 8:38pm UTC](https://discussions.unity.com/t/javascript-wordwrap-for-3d-text/401658 "2009-11-14T20:38:23Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![anon\_41226726](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@anon\_41226726](https://discussions.unity.com/u/anon_41226726)
#### Post date: [November 14, 2009, 8:38pm UTC](https://discussions.unity.com/t/javascript-wordwrap-for-3d-text/401658/1 "2009-11-14T20:38:23Z")

</div>

Just attach to your 3D text and set the properties, works fine for most purposes. Doesn’t account for Non-Mono-spaced font issues.

Feel free to improve upon it, I just needed this for a project and it met my needs and performance metrics.

```auto
// Word Wrap v1.0 for Unity 3D Text Objects
// 
// Author: Michael Colin Voigt
// [url]http://3037.com[/url]
// Free for all purposes, if you use it, just email me at [email]info@3037.com[/email]

public var textObject:GameObject;
public var wrapPoint = 0;
public var currentText:String;
public var hyphen = true;

function Awake () 
{

	currentText = " " + currentText;
	
	var finalString:String ="";

	var currentWrap = wrapPoint;
	var count = 0;
	var startPoint = 0;
	var lastBlankSpace = 0;
	var currentLength = 0;
	var appendText:String = "";

	while( count <= currentText.Length )
	{

		count++;
	
		if( count >= currentText.Length)
		{
				var finalLength = currentText.Length - startPoint;
				finalString += currentText.Substring( startPoint+1 , finalLength-1 );
				break;
		}
	
		if( currentText[count] == " " )
		{
			lastBlankSpace = count;
		}
	
		if( currentText[count] == " " count >= currentWrap )
		{
	
			currentLength = count - startPoint;
		
			appendText = currentText.Substring( startPoint+1 , currentLength ) + "\n" ;
			finalString += appendText;
		
			currentWrap = count + wrapPoint;
			startPoint = count;
	
		}
		
		if( currentText[count] != " " count >= currentWrap )
		{
			
			for(var i = count; i > startPoint;i--)
			{
				
				
				if( currentText[i] == " " ) 
				{
					
					
				Debug.Log(currentText[i]);
					
					currentLength = i - startPoint;
		
					appendText = currentText.Substring( startPoint+1 , currentLength ) + "\n" ;
					finalString += appendText;
		
					currentWrap = i + wrapPoint;
					startPoint = i;
	
				}
				
			}
		}
		
		
		if(hyphen)
		{
			var worldLength = (count - lastBlankSpace);
	
			if( worldLength > wrapPoint )
			{
				currentLength = count - startPoint;
		
				appendText = currentText.Substring( startPoint+1 , currentLength ) + "-\n" ;
				finalString += appendText;
		
				currentWrap = count + wrapPoint;
				startPoint = count;	
				lastBlankSpace = count;	
			}
		}
	}

textObject.GetComponent (TextMesh).text = finalString;

}

```

---

<div class="post-metadata">

### Author: ![Zubbe120](https://avatars.discourse-cdn.com/v4/letter/z/dec6dc/32.png) [@Zubbe120](https://discussions.unity.com/u/Zubbe120)
#### Post date: [March 25, 2014, 12:15am UTC](https://discussions.unity.com/t/javascript-wordwrap-for-3d-text/401658/2 "2014-03-25T00:15:02Z")

</div>

I hope no one minds if I bump this, but I was wondering if there was a way for the script to reset the wrapPoint when it detects a line break in the string. This script is working great so far, it’s just that every time I line break it still thinks that it’s in the same line as before.

DESIRED EFFECT:  
This is the first sentence.  
This is the second sentence.

CURRENT EFFECT:  
This is the first sentence.  
This is the  
second sentence.

UPDATE: I’ve figured out a way around it! I’ve added an if statement in the script after the hyphen if statement and before the get component part:

```auto
if( message[count] == "`" ){
     currentLength = count - startPoint;
     appendText = message.Substring( startPoint+1 , currentLength-1 ) + "\n" ;      
     finalString += appendText;
     currentWrap = count + wrapPoint;
     startPoint = count;
}

```

Now whenever I put `in the string, it will always line break and reset the count (wrapPoint) to 0. If you have to put` in your sentence, you could replace it with any symbol or letter you don’t really use like ~ or \*. Just make sure it’s ONE symbol, because if you put more than one it the if statement it won’t register.
