Search Unity

first char of string missed out sometimes...

Discussion in 'Scripting' started by San_Holo, Mar 26, 2018.

  1. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    135
    Hi, I've got a couple of text-mesh's for which I'm inserting strings into character by character like a Teletype machine... It works BUT occasionally the first character is missing from each string and I'm buggered as to why... I'm trying to clear out the string before inserting new data, but what's my problem... the char array needs resetting or should I do something else? - code follow, it's spaghetti-coding for the moment until I suss this small problem out, please advise...

    Code (csharp):
    1.  
    2. infoMessage1 = "Text for line one etc";
    3. infoMessage2 = "Text for line two etc";
    4. Invoke ("StartType", 0f);
    5.  
    6.     IEnumerator TypeInfoLine1 (float delay)
    7.     {
    8.         yield return new WaitForSeconds (delay);
    9.         typeText1 = "";
    10.         messageToType1 = ""; // clear this string first before we use it again
    11.         messageToType1 = infoMessage1;
    12.         infoMessage1 = ""; // empty the string once inserted into messageToType1
    13.         info_Line1.GetComponent<Renderer> ().enabled = true;
    14.         foreach (char message in messageToType1.ToCharArray()) {
    15.             typeText1 += message;
    16.             info_Line1.text = typeText1;
    17.             yield return new WaitForSeconds (letterPause);
    18.         }
    19.     }
    20.  
    21.     IEnumerator TypeInfoLine2 (float delay2)
    22.     {
    23.         yield return new WaitForSeconds (delay2);
    24.         typeText2 = "";
    25.         messageToType2 = ""; // clear this string first before we use it again
    26.         messageToType2 = infoMessage2;
    27.         infoMessage2 = ""; // empty the string once inserted into messageToType2
    28.         info_Line2.GetComponent<Renderer> ().enabled = true;
    29.         foreach (char message2 in messageToType2.ToCharArray()) {
    30.             typeText2 += message2;
    31.             info_Line2.text = typeText2;
    32.             yield return new WaitForSeconds (letterPause);
    33.         }
    34.     }
    35.  
    36. void StartType ()
    37.     {
    38.         info_Line2.text = ""; // lets clear the 2nd line when new text comes into line one
    39.         StartCoroutine (TypeInfoLine1 (0.0f));
    40.         StartCoroutine (TypeInfoLine2 (1.5f));
    41.     }
    42.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    7,056
    It's probably best to break the problem down into smaller pieces. It's likely that the pieces you have are interfering with each other. Even if that's not the cause, you don't want to copy and paste this function for every line of text, do you? And in any case, making a component that's a single line typer will make the problem easier to narrow down.

    So try this: Write a component that just outputs a single line of text to the Text component that's attached to itself. Isolate that component, just one line being typed. Now, in a new component, make that one a collection of these single lines. Or in programming terms, an array. This array can go through a bunch of there TextLineTypers and activate them in sequence.

    But start with getting a single line typer component working, then come back if you need more help.
     
    San_Holo likes this.
  3. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    135
    OK got you... well I fixed my problem, taking on board your advice I pushed all my text strings into one array and made just the one function to print the lines out, I was starting off my coroutine to start typing from within Update() so I was occasionally setting them off twice... a bool here & there sorted that out... I added the odd int to use as a marker to iterate through my strings array and bingo... no more foul ups... see below

    Code (csharp):
    1.  
    2.     public TextMesh info_Line1, info_Line2;
    3.     private string messageToType1, messageToType2;
    4.     private string infoMessage1, infoMessage2;
    5.     private string typeText1, typeText2;
    6.     public float letterPause = 0.01f;
    7.     private int messageMarker = 0;
    8.     private bool readyToType = true;
    9.     string[] teletypeMessages = {
    10.         "We shall be on the air every hour, on the hour, stay tuned to this channel.",
    11.         "The 'all clear' message will also be given on this channel.",
    12.         "Radioactive fall-out which follows a nuclear explosion is many times more dangerous,",
    13.         "Especially if you are directly exposed to it in the open.",
    14.         "Roofs and walls offer substantial protection. The safest place is indoors.",
    15.         "Make sure gas and other fuel supplies are turned off and that all fires are extinguished.",
    16.         "If mains water is available, this can be used for fire-fighting.",
    17.         "The mains water supply may not be available for very long.",
    18.         "You should also refill all your containers for drinking water after the fires have been put out,",
    19.         "Water must NOT be used for flushing lavatories.",
    20.         "Use your water only for essential drinking and cooking purposes.",
    21.         "Water means life, Don't waste it.",
    22.         "Make your food stocks last, ration your supply, because it may have to last for 14 days or more.",
    23.         "If you have fresh food in the house, use this first to avoid wasting it, food in tins will keep.",
    24.         "If you live in an area where a fall out warning has been given,",
    25.         "Stay in your fall-out room until you are told it is safe to come out.",
    26.         "When the immediate danger has passed the sirens will sound a steady note.",
    27.         "The 'all clear' message will also be given on this channel.",
    28.         "If you leave the fall-out room to go to the lavatory or replenish food or water supplies,",
    29.         "Do NOT remain outside the room for a minute longer than is necessary.",
    30.         "Do NOT in any circumstances go outside the house, radioactive fall-out can KILL.",
    31.         "You cannot see it or feel it, but it is there.",
    32.         "Stay in your fall-out room until you hear the 'all clear' on the sirens.",
    33.         "If you go outside, you will bring danger to your family and you may die.",
    34.         "We shall be on the air every hour, on the hour.",
    35.         "Stay tuned to this channel. That is the end of this broadcast."
    36.     };
    37.  
    38. void Update ()
    39. {
    40. //
    41. // INFO TEXT TIMINGS
    42. //
    43.         if (counterRunning && timerDigit1 == 3 && timerDigit2 == 5 && timerDigit3 == 0 && timerDigit4 == 0 && timerDigit5 < 2) { // call at 3:50:00
    44.             // first info messages are played in BeginCountdown ()
    45.             if (readyToType) {
    46.                 TypeMessages ();
    47.             }
    48.         }
    49. }
    50.  
    51.     IEnumerator Teletype ()
    52.     {
    53.         messageToType1 = teletypeMessages [messageMarker];
    54.         messageToType2 = teletypeMessages [messageMarker + 1];
    55.         foreach (char message in messageToType1.ToCharArray()) {
    56.             typeText1 += message;
    57.             info_Line1.text = typeText1;
    58.             yield return new WaitForSeconds (letterPause);
    59.         }
    60.         foreach (char message2 in messageToType2.ToCharArray()) {
    61.             typeText2 += message2;
    62.             info_Line2.text = typeText2;
    63.             yield return new WaitForSeconds (letterPause);
    64.         }
    65.         typeText1 = "";
    66.         typeText2 = "";
    67.         messageMarker = messageMarker + 2;
    68.         readyToType = true;
    69.     }
    70.  
    71.     void TypeMessages ()
    72.     {
    73.         readyToType = false;
    74.         info_Line1.text = "";
    75.         info_Line2.text = "";
    76.         StartCoroutine (Teletype ());
    77.     }
    78.  
     
unityunity