How to implement Text Mesh text wrapping?

Hello,

I’ve created a keyboard in-game that the player can type with and I’m currently using a text mesh to represent the the text they type. I’m trying to limit the number of characters that can be entered on a single given “line” in the mesh. The idea will be that if a line runs too long, the script will find the next space after it goes over the limit and replace it with a line break. However, while making this I have ran into a few problems. The primary issue I’ve been running into is the “Cannot apply indexing with to an expression of type ‘method group’” error as well as the “Cannot convert type ‘char’ to ‘string’” error. Here’s what I have so far:

using UnityEngine;

using System.Collections;


public class TextWrap : MonoBehaviour {


	// Use this for initialization

	public TextMesh text;
	void Start () {


	}
	// Update is called once per frame

	void Update () {

	
	text.text = ApplyWrap(text.text, 14);
	}
	private string ResolveTextSize(string input, int lineLength){
		int i=0;
		while((string)input*!='

'){*

  •  	i++;*
    
  •  	if(i>=lineLength){*
    

_ if(input*==’ ‘){_
_ input=’
';
}//end if*
* }//end if*
* }//end while*
* return input;
}//end ApplyWrap*
}//end TextWrap
Thanks, Jesse_

Hi Jesse,
I took a look at your code, but, it is difficult to give you a complete answer without knowing how you are calling the ResolveTextSize and what ApplyWrap is. The code below can’t really be tested until those pieces of information are evident, but, there are somethings that are altered that may help :

using UnityEngine;
using System.Collections;

public class TextWrap : MonoBehaviour {

	// Use this for initialization
	public TextMesh text;
	void Start () {
		
    }
	// Update is called once per frame
	
	void Update () {
		text.text = ApplyWrap(text.text, 14); //What is applywrap?
	}

	//what calls ResolveTextSize?
	private string ResolveTextSize(string input, int lineLength){
		for( int i = 0; i < input.Length; i++){
			if(i>=lineLength){
				if(input*==' '){*

_ input*="
";_
_
}//end if*_
* }//end if*
* }//end for*
* return input.ToString;*
* }//end ApplyWrap*
}//end TextWrap
For one, it is usually better to use a for loop if all of the information is already available and being passed into a function, the ‘.length’ method will give you the int you need.
The ‘.toString’ Method may help out your char not a string issue…maybe, again that is an issue of insufficient information. But, if it isn’t that it may be how you are passing the input to the function.

if(input*==’ '){*
input*="
";*

}//end if
You’re not allowed to assign a string into a char.
input = char
"
" = string