Animating HUD element difficult puzzle!

I have tried to figure this out for a day:


Here is the relevant code in order of execution, I have put in if statements everywhere hoping to not have the animations conflict with each other. The script is run from the player by the way, not from each individual diamond. The one diamond collected is a public bool. I tried making it static so it’s used by all scripts but that didn’t seem to fix anything. There is clearly some conflict going on but I can’t see what it is. Anyone who sees what the problem is?

 void OnTriggerEnter(Collider Triggering)    
    {
        if (Triggering.gameObject.tag == "Diamond")
        {
            Diamond.Play();
            diamondsCollected += 1;
            DiamondCollectTime();
        }
    }


     void DiamondCollectTime()
    {

        if(oneDiamondCollected == false)
        {
            TextAnimation.GetComponent<Animation>()["DiamondCountAnimation"].speed = 1;
            TextAnimation.GetComponent<Animation>().Play("DiamondCountAnimation");
            oneDiamondCollected = true;
        }
	else if(oneDiamondCollected == true)
	{


	}



        if (oneDiamondCollected == true)
        {
            StartCoroutine("CollectionDisappear");
        }



 IEnumerator CollectionDisappear()
    {
	if(oneDiamondCollected == true)
	{
        yield return new WaitForSeconds(3);
        TextAnimation.GetComponent<Animation>()["DiamondCountAnimation"].speed = -1;
        TextAnimation.GetComponent<Animation>().Play("DiamondCountAnimation");
        oneDiamondCollected = false;
}
else if (oneDiamondCollected == false)
{
yield break;
}
    }

Hey, it’s Will. You linked this post on my youtube video (Juicier UI - Make Your Menus and HUD Animated! (Unity Tutorial) - YouTube) and I figured it would be easier to reply here.

Honestly I’m not entirely sure whats going on here. I would probably need to see more of your code to know what all is going on.

If I had to take a stab at it, based on this code and your video, You might have some weirdness going on with overlapping co-routines.

The best way I’ve found to troubleshoot these kinds of issues in Unity, is to use print statements to flag parts of the code. That way I can step through what exactly is happening, and see whats going on while the game is running. So I might put one print saying “Collided with diamond” “Started Disappear Co-Routine” etc, that you can see what is happening. You can delete them after you fix the issue, so your console isn’t a mess.

Hope that was helpful!,Hey, it’s Will. You linked this post on my UI youtube video and I figured it would be easier to post here.

Honestly I’m not exactly sure what’s wrong here. I’m guessing I would need to see more of what’s going on with the code to tell for sure.

If I had to take a stab at it, based on this code and the video, I’d say there’s some weird stuff going on with overlapping co-routines. The best way I’ve found to pin point issues like this, is to put some print statements around in the code to flag when certain actions take place. Like “started disappear co-routine” “Collided with diamond” etc, at anywhere that makes sense that you can see what your code is doing in the console while it runs.