Search Unity

Need help: How to loop 2 Strings to fade in/out after one an other?

Discussion in 'Scripting' started by Lazius001, Jul 24, 2021.

  1. Lazius001

    Lazius001

    Joined:
    Mar 5, 2020
    Posts:
    7
    HI I am stuck on this tutorial for the Junior Programming, I tried so many types of permutations from FoorLoops, InvokeRepeating, and I can't get these these two texts to fade in and out after one and other in a repeating loop. here's the code (I also spent several hours searching online and can't find any info for it.
    - They fade in/out only once at start, but don't repeat - appreciate any help.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class CongratScriptDebugged : MonoBehaviour
    {
    public TextMesh Text;
    public ParticleSystem SparksParticles;

    private List<string> TextToDisplay = new List<string>();

    private float RotatingSpeed = 2.0f;
    private float TimeToNextText;

    private int CurrentText;

    // Start is called before the first frame update
    void Start()
    {
    Text = GetComponent<TextMesh>();

    TextToDisplay.Add("Congratulation");
    TextToDisplay.Add("All Errors Fixed");
    Text.text = TextToDisplay[0];

    SparksParticles.Play();
    }

    // Update is called once per frame
    void Update()
    {
    InvokeRepeating("RepeatLoop", 1.0f, 2.0f);
    }
    void RepeatLoop()
    {

    TimeToNextText += Time.deltaTime * RotatingSpeed;

    if (TimeToNextText > 1.5f)
    {
    CurrentText++;
    }
    if (CurrentText >= TextToDisplay.Count)
    {
    CurrentText = 1;

    Text.text = TextToDisplay[CurrentText];
    }

    }

    }
     
  2. Lazius001

    Lazius001

    Joined:
    Mar 5, 2020
    Posts:
    7
  3. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    348
    Please use code tags on the forum when making a post. It makes things easier to read and talk about :)

    I don't see anywhere in your code where you set TimeToNextText back to zero.

    If you don't reset your timer, it'll just keep being > 1.5 seconds forever.
     
  4. Lazius001

    Lazius001

    Joined:
    Mar 5, 2020
    Posts:
    7
    oh I see let me research online how to reset the timer.
     
  5. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    348
    Just setting TimeToNextText = 0 at the right time will do it i think ;)

    Something like,
    if TimeToNextText is < 1.5, show first text
    if TimeToNextText is between 1.5 and 3.0, show the second text
    if TimeToNextText is > 3,0 set TimeToNextText = 0

    Probably the the most straight-forward way to go and I think accurate enough for what you want. No need for crazy complexity.

    Side note: I know OP said he'll research online to do this and didn't ask for an answer, but I wanted to reply with at least some pseudo code because I worried I might have sent him in a needlessly complicated direction by using the word 'timer' in my first reply. If he's off googling timers, I doubt many of those results would help with this particular case.
     
  6. Lazius001

    Lazius001

    Joined:
    Mar 5, 2020
    Posts:
    7
    Sweet thanks let me try this :) - and yes you are right, seems I am not at a level to understand yet what to search for/not to search for at the moment - seems all answers would be crazy complicated lol!
     
    mopthrow likes this.