the following Code after "yield return new WaitForSeconds();" not executed
Its like this, i have project, with some cool items. One of them is the Black Hole and another one is the "Hulk mode". The Black Hole reduce the time (with the Time.timeScale = 0.3f; command) too 0,3f and the Hulk Mode sets the size of the GameObject to a bigger Size and cant get hurt from Enemies. But when I integrate my Black Hole item in the Game the command "yield return new WaitForSeconds();" wont work anymore (or the following code after it). Does anyone have some ideas what went wrong? Tips for my Scritping Skills are Welcome too ;D
here's the Code for the Black Hole:
public class SlowMotion : MonoBehaviour
{
IEnumerator OnCollisionEnter2D (Collision2D collision)
{
if(collision.gameObject.tag == "rocket")
{
Debug.Log("SlowMotion startet");
Time.timeScale = 0.3f;
yield return new WaitForSeconds(2f);
Time.timeScale = 1.0f;
Debug.Log("SlowMotion Ende");
}
}
}
here the code for the Hulk Mode:
public GameObject pickupEffect;
public float mutliplier = 1.4f;
public float scale;
public float duration;
protected float Counter;
float trueornot;
BoxCollider2D collider;
protected void CountCounter()
{
Counter = trueornot;
}
void Start()
{
trueornot = 1f;
}
void OnTriggerEnter2D (Collider2D other)
{
if(other.CompareTag("rocket"))
{
StartCoroutine(Pickup(other));
}
}
private IEnumerator Pickup(Collider2D rocket)
{
Instantiate(pickupEffect, transform.position, transform.rotation);
pickupEffect.transform.localScale = new Vector3(2,2,2);
GetComponent<BoxCollider2D>().enabled = false;
rocket.transform.localScale = new Vector3 (scale, scale, 1);
rocket.gameObject.tag="Player";
Debug.Log("1");
yield return new WaitForSeconds(duration);
Debug.Log("2");
rocket.gameObject.tag="rocket";
rocket.transform.localScale = new Vector3 (2, 2, 2);
Destroy(this.gameObject);
}
}
I have a third Item, and with it I had some issues too, the position from the instantiated Objects were the same position where I collected the first Item. But this Issues got fixed by setting a "private" in front of the IEnumerator, maybe this comment is a help for the troubleshooting.
sincerely
Niklas
Answer by xxmariofer · Aug 27, 2020 at 06:50 AM
almost always this errors happens because you are deleting or desactivating the gameobject this script is attach to, take into account that if you delete the gameObject, it wont continue after the yield return
Alright, so how can I avoid this problem? $$anonymous$$aybe with a second script or should i put the "Destroy(this.gameObject);" behind the "StartCoroutine(Pickup(other));" ?
the Destroy(this.gameObject) is getting executed after the yield so you might be destroying to object somewhere else
Your answer
Follow this Question
Related Questions
Farming game Time management 0 Answers
Slow Motion Issues! 1 Answer
Trigger a Timer 2 Answers
Enemy waits 5 seconds after attack 0 Answers
Time.deltaTime Stuck Camera 1 Answer