E
E
ellz2019-01-16 19:39:00
C++ / C#
ellz, 2019-01-16 19:39:00

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);
        }
    }

But on startup I get an error -
ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: startIndex

When you click on it, it does not point to the place in the code where it appears from.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2019-01-16
@ellz

ArgumentOutOfRangeException
startIndex is negative or greater than the length of this instance.

From here - https://docs.microsoft.com/en-us/dotnet/api/system...
To insert at position j (which on the first iteration of the outer loop will be from 0 to needText.Length) in the line maintext.Text there should already be a sufficient number of characters - i.e. needText.Length.
Moreover - inserting an empty string does not make sense. Delete via substring, for example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question