Line of code skips randomly at times?

So the whole script is pretty long but basically I have an object which when you tap it calls the currency method:

if (Input.touchCount >= 1) {

						hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint ((Input.GetTouch (0).position)), Vector2.up);

						//On touch
						if (hit.collider != null && hit.collider.tag == "bug" && Input.GetTouch (0).phase == TouchPhase.Began) {
			
								Currency();
								hit.transform.gameObject.SetActive (false);
								changesprite ();
								spritetransform = true;
								
						}

This is my code inside my currency method:

if(isapple == true)
			{
				A1.GetComponent<SpriteRenderer>().sprite = apple;
			}

			if(iscoin == true)
			{
				A1.GetComponent<SpriteRenderer>().sprite = coin;
			}

			C1.transform.position = new Vector2(hit.transform.position.x, hit.transform.position.y);
		
			StartCoroutine(C1handler());

			return;

The problem is inside my coroutine:

	public IEnumerator C1handler()
	{
		C.SetBool("startABT", true);
		C1avail = true;

		yield return new WaitForSeconds (0.1f);

		Debug.Log("time exceeded");
		C.SetBool("startABT", false);
		C1avail = true;
}

Most of the time the game randomly skips calling this line:

C.SetBool("startABT", false);

Anyone know what the problem could be? Any ideas/answers are greatly appreciated. Thanks! :slight_smile:

There’s not way a line of code could be “skipped randomly” unless you put that line inside some random condition that might skip it.

Since you’re working with coroutines note that deactivating or destroying the object that started the coroutine will stop it (you can’t start coroutines on disabled objects for the same reason). Maybe the code it’s not skipping that line, maybe the coroutine has stopped completelly, or the next line is always executed?

Also, the line might be called, but another line in another script might set it to true again in the same frame.

Now, I see you have a debug line that prints “time exceeded”, that line is always printed? What if you add another debug line below that line. If only the first line is called and not always then your coroutine is being stopped, if both lines are printed then the line between them has been called and the problem is somewhere else (like setting it to true in another script).