[Solved] AutoType script doesn´t work!

(Maybe my English isn´t perfect)
Hi, I want to use this script, but with the JS version I get an error and the CS version does nothing.

The error: “ToCharArray” is not a member of “Object”

var letterPause = 0.2;
var sound : AudioClip;
 
private var word;
 
function Start () {
	word = guiText.text;
	guiText.text = "";
	TypeText ();
}
 
function TypeText () {
	for (var letter in word.ToCharArray()) {
		guiText.text += letter;
		if (sound)
			audio.PlayOneShot (sound);
		yield WaitForSeconds (letterPause);
	}		
}

And the CS version does nothing, the text will be displayed normally (Not letter by letter)

CS:

using UnityEngine;
using System.Collections;
 
public class AutoType : MonoBehaviour {
 
	public float letterPause = 0.2f;
	public AudioClip sound;
 
	string message;
 
	// Use this for initialization
	void Start () {
		message = guiText.text;
		guiText.text = "";
		StartCoroutine(TypeText ());
	}
 
	IEnumerator TypeText () {
		foreach (char letter in message.ToCharArray()) {
			guiText.text += letter;
			if (sound)
				audio.PlayOneShot (sound);
				yield return 0;
			yield return new WaitForSeconds (letterPause);
		}      
	}
}

Thanks in advance…

take a look at [this][1] which has helped others in the past. [1]: http://answers.unity3d.com/questions/735306/auto-type-text-does-not-change-to-the-next-sentenc.html

If you would say what error exactly you get maybe it would be easier for us to help.

Sorry, I forgot to write what line the error is. Im not on my Computer at the momen, I'll post it later

1 Answer

1

Javascript doesn’t have the concept of chars. Just use strings. I change little your JS script:

 var letterPause = 0.2;
 var sound : AudioClip;

 private var word: String; //declare type

 function Start () {
  word = guiText.text;
  guiText.text = "";
  TypeText ();
 }

 function TypeText () {
  for(var i: int = 0; i < word.Length; i++) {
   guiText.text += word.Substring(i, 1);
   if (sound)
    audio.PlayOneShot (sound);
   yield WaitForSeconds (letterPause);
  }       
 }

I hope that it will help you.

Thank you!