- Home /
Using WaitForSeconds and trigger
I'm trying to make it so on a trigger it starts the coroutine and in the IEumerator i want to use waitforseconds to do something.
Right now I can do the StartCoroutine("my method"()); in the trigger event and it works up until the waitforseconds method.
using UnityEngine; using System.Collections;
public class PowerUpFire : MonoBehaviour { public bool isHit = false; // Use this for initialization void Start() {
}
// Update is called once per frame
void Update()
{
}
void test()
{
Debug.Log("TEst");
}
void OnTriggerEnter2D(Collider2D collision)
{
Destroy(gameObject);
PlayerController.health = 500f;
startIn();
}
public void startIn()
{
StartCoroutine(waitCouple());
}
IEnumerator waitCouple()
{
print("tda");
Debug.Log(Time.time);
yield return new WaitForSeconds(3);
Debug.Log("ADD");
}
}
The problem is that you're calling the coroutene function every frame the object is inside the trigger. That's it will never print "ADD". Because before the 3 seconds are done, you're starting the coroutene function again.
The problem is that you're calling the coroutene function every frame the object is inside the trigger. That's it will never print "ADD". Because before the 3 seconds are done, you're starting the coroutene function again.
He is using OnEnter, not OnStay and even if he was infact using OnStay and thus calling the coroutine every frame, it sure would print "ADD", because this:
StartCoroutine(waitCouple());
Just starts a new instance of the coroutine and it doesn't prevent the last one from running
Yeah my mistake. I don't know how i missed that. And yeah, that was exactly what i was trying to tell him assu$$anonymous$$g that he used OnTriggerStay. Cheers!
Answer by MacDx · Oct 02, 2017 at 10:36 PM
The issue is the placement of this line:
Destroy(gameObject);
You are marking the object this script is attached to, to be destroyed, but then you start a coroutine? That will never work. Your coroutine works up to WaitForSeconds because up to that point the script running it still exists, but when the 3 seconds have passed, the script doesn't exist anymore so it can't finish running.
What you have to do is wait until the coroutine finishes and, then and only then, destroy the game object. So remove that Destroy line and put it at the end of your coroutine.
Can i make it so the gameobject is not visable but is still there. Then destroys after the seconds? if so how?
You sure can! The way to do it depends on the type of object. In your case I assume it is a sprite since you are the 2D trigger methods. What you can do is get a reference to the Sprite Renderer component and then set the color's alpha value to 0. Something like this:
GetComponent<SpriteRenderer>().color = new Color(1f,1f,1f,0);
This will make the sprite invisible
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Another question about yield, WaitForSeconds in c# 2 Answers
C# WaitForSeconds doesn't seem to work ?? 2 Answers
Allowing only one jump? 2 Answers