- Home /
AutoType script problem.
I am trying to make a small cutscene with some text shown in the bottom of screen I want the text to appear one word at a time like in RPG games. I tried using the AutoType script, it works fine when I put it on the guitext and put the text in the guitext using inspector. But when I use a script like
var textString : GUIText;
function Start {
textString.guiText.text = "Testing";
}
it doesn't work like its supposed to work with the AutoType script. For who don't know about AutoType here is the link and code itself.
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);
}
}
Answer by $$anonymous$$ · Jan 12, 2013 at 03:19 PM
This is because in the Start()
method the script actually runs some logic and it launches the TypeText()
coroutine. However when you change the text
property manually, you do not trigger the effect, TypeText()
isn't called. This is what I advise:
var letterPause = 0.2;
var sound : AudioClip;
private var message;
function Start () {
SetText(guiText.text);
}
function TypeText () {
for (var letter in message.ToCharArray()) {
guiText.text += letter;
if (sound)
audio.PlayOneShot (sound);
yield WaitForSeconds (letterPause);
}
}
function SetText(text : string)
{
StopCoroutine("TypeText");
message = text;
guiText.text = "";
StartCoroutine("TypeText");
}
And from now on, use SetText("message");
to change the text, and it will even stop the current AutoTyping and start it over with the new text.
I am using another script attached to another object to pass the 'string' data to the guittext and I attached this script to the guittext and it gave me errors in line 'function settext(text : string)' here i changed it to String then it gave error in line 12 tochararray not member of object, I changed private var message : String; but now its no error but just same story don't work
What do you mean by "don't work"? This works for me, I tested it. If you get a reference to the AutoType component, you can set it from any other script and it automatically does the AutoTyping thing. But you $$anonymous$$UST use SetText(), don't set the guiText.text directly. Can you show me how you're using this from another script? You can use it like this:
someGUITextObject.GetComponent(AutoType).SetText("blabla")
or:
var typer = someGUITextObject.GetComponent(AutoType); typer.SetText("blabla");
GOT IT! I was doing this //var text = textString.GetComponent(autotype); //text.message = "What is happening";
Thank you. Awesome Answer