- Home /
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! :)
How do you know it skips calling the line , have you checked with Debug.Log above the "WaitForSeconds". Are you sure its not because of the fact that you reset the value of "startABT" after just 0.1 seconds
no, I'm ssure it skips because I look at the animator while I test on unity remote. Also it still skips even if I change it to 1 or 2 secs after (my main goal is to have it at 1 sec though)
Answer by DiegoSLTS · Jun 04, 2015 at 02:09 PM
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).
Hi Diego, Thanks for the informative answer, I will investigate when I return home(I'm at school right now). I just realized that the problem is probably due to the fact that I disable my object after calling the currency method. So, like you said, the coroutine is probably being interrupted.
Still couldn't find a solution. I even put the whole 'turn object false' piece of code at the end of the coroutine, but the result is the same.
public void CreateInternet$$anonymous$$atch()
{
if(Password == null || Password == "")
{
if(roomName != null || roomName != "")
{
Network$$anonymous$$anager.singleton.match$$anonymous$$aker.Create$$anonymous$$atch(roomName,4, true,"", "", "", 0, 0, OnInternet$$anonymous$$atchCreate);
Debug.Log(roomName+ " public");
}
Status1.text = "Please enter a name for your room";
}
else
{
if (roomName != null || roomName != "")
{
Network$$anonymous$$anager.singleton.match$$anonymous$$aker.Create$$anonymous$$atch(roomName,4 , true,Password, "", "", 0, 0, OnInternet$$anonymous$$atchCreate);
Debug.Log(roomName + " Private");
}
Status1.text = "Please enter a name for your room";
}
}
than explain why Status1.text is not changing and yes I attached it to the right gameobject
Your answer
Follow this Question
Related Questions
making a 2D character Speak for different lenghts of time 2 Answers
How to prevent holding down a key to infinitely loop an action? 1 Answer
Yield Wait for Seconds (A little help here) 2 Answers
AnimationCurve as fixed time rather than portion of time? 1 Answer
Mecanim animation, playing once 1 Answer