The code after WaitForSeconds is not working
Hello everyone. I'm making a runner game, so I need a powerup that makes you jump higher for 3 seconds. I've made a sprite with a box collider trigger that starts the DoubleJump coroutine. There's the code of the script that's attached to the sprite:
private void OnTriggerEnter(Collider other)
{
Destroy(GetComponent<Transform>().gameObject);
if (other.CompareTag("Player")) {
StartCoroutine(DoubleJump(other));
}
}
IEnumerator DoubleJump(Collider other)
{
var obj = other.GetComponent<RigidbodyFirstPersonController>().movementSettings;
obj.JumpForce *= 2;
Debug.Log("before return");
yield return new WaitForSeconds(1);
Debug.Log("after return");
obj.JumpForce /= 2;
}
After colliding with the sprite it disappears, jump force is doubling, "before return" shows in a log but nothing really happens after that, so the code after "yield return new WaitForSeconds(1);" is not evaluating. Can you please tell me why and fix the problem? Thank you.
if you destroy the gameObject that the script is attached to then the script is destroyed too. in your OnTrigger disable the sprite and destroy the gameObject when coroutineis finished.
Wait a $$anonymous$$ute... if I disable a gameobject, I can't start a coroutine...
private void OnTriggerEnter(Collider other)
{
GetComponent<Transform>().gameObject.SetActive(false);
if (other.CompareTag("Player")) {
StartCoroutine(DoubleJump(other));
}
}
IEnumerator DoubleJump(Collider other)
{
var obj = other.GetComponent<RigidbodyFirstPersonController>().movementSettings;
obj.JumpForce *= 2;
Debug.Log("before return");
yield return new WaitForSeconds(1);
Debug.Log("after return");
obj.JumpForce /= 2;
Destroy(GetComponent<Transform>().gameObject);
}
It shows an error: "Coroutine couldn't be started because the the game object '2x-jump-powerup' is inactive!"
Don't disable the gameObject only the sprite
private void OnTriggerEnter(Collider other)
{
GetComponent<Image>().enabled = false;
if (other.CompareTag("Player")) {
StartCoroutine(DoubleJump(other));
}
}
IEnumerator DoubleJump(Collider other)
{
var obj = other.GetComponent<RigidbodyFirstPersonController>().movementSettings;
obj.JumpForce *= 2;
Debug.Log("before return");
yield return new WaitForSeconds(1);
Debug.Log("after return");
obj.JumpForce /= 2;
Destroy(gameObject);
}
Answer by Hellium · Oct 03, 2017 at 02:04 PM
Make the RigidbodyFirstPersonController
run the coroutine instead of the power up :
if (other.CompareTag("Player")) {
other.GetComponent<RigidbodyFirstPersonController>().StartCoroutine(DoubleJump(other));
}
Oops, my bad. Only $$anonymous$$onoBehaviours can run coroutines. Try this ins$$anonymous$$d :
other.GetComponent<RigidbodyFirstPersonController>().StartCoroutine(DoubleJump(other));
Your answer
Follow this Question
Related Questions
Add points every five seconds 1 Answer
Need help using coroutines 1 Answer
Coroutine Error when Capturing 360 Video 0 Answers
IEnumerator Not Completing Entire Function 0 Answers
Stopping a Coroutine, starting a new one 2 Answers