Hello everybody, I hope you’re fine.
I need some help with one of my script. I try to disable my score from the screen but I have a difficulty.
When I take a piece on my level, I make my score appear. Then, I disable him with a coroutine.
When it’s happening the first time, all works fine. But when the player take an another piece, the score disappear but to speedly. And when the player take a third piece, the score disappear much quickly again.
I don’t know why…
static float FadeOutSpeed = 0.005f;
public void AddOnePoint()
{
countPoints += 1;
print("Nombre de points " + countPoints);
StartCoroutine(DisableScore());
Color ci = imagePoint.color;
ci.a = 1;
imagePoint.color = ci;
scoreText.text = "" + countPoints + " / " + totalPoints;
scoreText.color = new Color(0, 0, 0);
}
IEnumerator DisableScore()
{
while (true)
{
imagePoint.color -= new Color(0, 0, 0, FadeOutSpeed);
scoreText.color -= new Color(0, 0, 0, FadeOutSpeed);
yield return null;
}
}
Thanks for your help.
Sorry for my english, I’m not very good at it.
I Guess The speed problem of your score is with your FadeOutSpeed which is being increased on every CountPoints increment.
also, it could be that the DisableScore routine doesn’t terminate, ever, thus running twice (and fading twice as fast) on the second call, and three times on the third call.
i think you’d want to have it break out of the while(true) loop when the colors alpha reaches 0 and/or StopCoroutine() it before Starting it again, like so
// in Awake, etc
IEnumerator fader = DisableScore ();
// in AddOnePoint instead of StartCoroutine:
StopCoroutine (fader);
StartCoroutine (fader);
Hi
First, thanks for you help. I try to use break; like you recommand Asim, at the end of DisableScore(); but it’s doesn’t work well. The score don’t disapear, it’s just stay, maybe I can use break; after several seconds ?
I also try to use your solution Raziel but it’s doesn’t change anything. I write IEnumerator fader = DisableScore (); in AddOnePoint because if I try to initialise it in Awale, StopCoroutine and StartCoroutine can’t find it. Maybe I don’t do it right.
I think I’ll try to use break; at the end of my function after x seconds. Maybe it’s the solution.
Thanks again for your posts. It’s really interesting to read other potential possibilities.
EDIT : Hmmmm… I’m not finding what to do in fact. ._.’
you have to save it outside of AddOnePoint (like in Awake/Start/OnEnable/etc), beacuse if you create it inside, you’ll create a new coroutine instance each time, and one you cannot stop, since it’s not running yet
(there’s a better example at: Unity - Scripting API: MonoBehaviour.StopCoroutine )
I don’t know why I have some trouble with that… Mayve it’s because I place this in my Gamecontroller script. Do I have to create an another script ?
I’ll try to do this tomorow.
Thanks again Raziel, I’m sure you’re right. I’dont lose hope.
theres a few ways you can restart a coroutine. while you can stop the coroutine early (see my example) for short coroutines such as this one that shouldn’t matter. what really matters is properly terminating that loop. that while(true)
is the real problem.
instead provide a loop condition that eventually reaches an end
Coroutine handle;
Color clear = new Color(0,0,0,0);
public void AddOnePoint()
{
countPoints += 1;
print("Nombre de points " + countPoints);
if(handle!=null)
StopCoroutine(handle);
handle = StartCoroutine(DisableScore());
scoreText.text = "" + countPoints + " / " + totalPoints;
scoreText.color = new Color(0, 0, 0);
}
IEnumerator DisableScore()
{
float colorLerp =0;
while (colorLerp<1)
{
colorLerp = Mathf.MoveTowards(colorLerp,1,FadeOutSpeed * Time.deltaTime);
imagePoint.color = scoreText.color = Color.Lerp(Color.Black,clear,colorLerp);
yield return null;
}
}
also take note of the “Time.deltaTime”. the loop you had was frame-dependentmeaning the fading would be choppy at bad frame rates, or super fast at high framerates. the Time.delta time will make its fade realtime (unless you’ve messed with the timescale for some slowmotion or fast forward effects).
not saying you shouldn’t ever make an infinite loop coroutine, just wait until you’re really confident in coding them cause they can bite you
Oh yes Joshua, your help is perfect.
I just add some changes to don’t make my image becoming black, but it’s perfect. I use that code to make it happen the way I want.
Coroutine handle;
public void AddOnePoint()
{
countPoints += 1;
print("Nombre de points " + countPoints);
if (handle != null)
StopCoroutine(handle);
handle = StartCoroutine(DisableScore());
scoreText.text = "" + countPoints + " / " + totalPoints;
scoreText.color = new Color(0, 0, 0);
Color ip = imagePoint.color;
ip.a = 1;
imagePoint.color = ip;
}
IEnumerator DisableScore()
{
float colorLerp = 0;
Color ip = imagePoint.color;
ip.a = 0;
imagePoint.color = ip;
while (colorLerp < 1)
{
colorLerp = Mathf.MoveTowards(colorLerp, 1, FadeOutSpeed * Time.deltaTime);
scoreText.color = Color.Lerp(scoreText.color, ip, colorLerp);
imagePoint.color = Color.Lerp(imagePoint.color, ip, colorLerp);
yield return null;
}
Thanks again ! Two days I’m stuck because of this. It’s great to find some help here !