Answer the question
In order to leave comments, you need to log in
Trouble creating a text typing effect?
I create the effect of typing text. The bottom line is this: There is a set of random characters in a string, a string with the desired text and two variables SpeedChanging (the speed of changing the random text), SpeedToSet (the speed of setting a character from the required string). It is necessary that the random text is re-generated with the speedChanging speed, and the required text from NeedText is set from left to right with the SpeedToSet speed.
For example:
needText = "Desired text"
First pass:
string result = akfgmassnvkczxvxzklc
Second pass:
string result = N fasdkmfgkvmxzckvvcs
Third pass:
string result = Well vbxcl,b;vxcmbmxcvsa
Fourth pass:
string result =Need vbxcl ,b;vxcmbmxcvs
and so on.
Here is how I am trying to do it:
[SerializeField]
private Text mainText;
[SerializeField]
private string needText;
private const string chars = "QqWwEeRrTtYyUuIiOoPpAaSsDdFfGgHhJjKkLlZzXxCcVvBbNnMmЙйЦцУуКкЕеНнГгШшЩщЗзХхЪъФфЫыВвАаПпРрОоЛлДдЖжЭэЯяЧчМмИиТтЬьБбЮю,.?/|\';:]{}[123456789+-*ҐґЄЇїє";
[SerializeField]
private int lenght;
[SerializeField]
private float speedChanging;
[SerializeField]
private float speedToSet;
private void Start()
{
//mainText.text = GenerateRandom(lenght);
StartCoroutine(GenerateString());
}
private string GenerateRandom(int lenght)
{
Random random = new Random();
string result = "";
for (int i = 0; i < this.lenght; i++)
{
result += chars[Random.Range(0, chars.Length)];
}
return result;
}
IEnumerator GenerateString()
{
for (int i = 0; i < needText.Length; i++)
{
mainText.text += needText[i];//генерирую нужный символ
for (int j = i; j < needText.Length; j++)
{
mainText.text = mainText.text.Insert(j, "");//удаляю все что не нужно
}
mainText.text += GenerateRandom(needText.Length-i);// вставляю рандомные символы за нужными символами
yield return new WaitForSeconds(speedChanging);
}
}
ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: startIndex
Answer the question
In order to leave comments, you need to log in
ArgumentOutOfRangeException
startIndex is negative or greater than the length of this instance.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question