I am new to Unity and C#, I am trying to add a 'wait time' to my death script. So when my Player dies it shows a particle animation and then resets the level, however, at the moment my particles are playing but the level doesn't reset.
public GameObject particles;
public Player_Movement player;
void OnCollisionEnter2D(Collision2D Col)
{
if (Col.collider.tag == "Enemy")
{
Instantiate(particles, transform.position, Quaternion.identity);
Destroy(gameObject);
StartCoroutine("RestartGameCo");
}
}
public IEnumerator RestartGameCo()
{
yield return new WaitForSeconds(0.5f);
SceneManager.LoadScene("Level1");
}
Destroy(gameObject);
StartCoroutine("RestartGameCo");
Your code is fine, but you destroy the gameobject that has this script on it. Which also destroys the script, and stops all the coroutines. So it will never be called.
A solution is to make the object invisible in some way, like disabling the mesh renderer and collider instead of destroying it.
You are actually destroying the gameobject itself before even running the co-routine. It means that the co-routine "RestartGameCo" won't even run. One way to debug these kind of things is using Debug.log messages.
Code:
void OnCollisionEnter2D(Collision2D Col)
{
if (Col.collider.tag == "Enemy")
{
Instantiate(particles, transform.position, Quaternion.identity);
StartCoroutine("RestartGameCo");
}
}
public IEnumerator RestartGameCo()
{
yield return new WaitForSeconds(0.5f);
SceneManager.LoadScene("Level1");
}
Let me know if it helps.
I know a couple options about that. Here's one.
By using the Thread, it will pause your application for the amount of time of your desire. To do so, you need to add using System.Threading; to your the current cs file you are editing. Now simply change your code to that:
void OnCollisionEnter2D(Collision2D Col)
{
Thread.Sleep(5000) // 5000 in milliseconds
if (Col.collider.tag == "Enemy")
{
Instantiate(particles, transform.position, Quaternion.identity);
Destroy(gameObject);
StartCoroutine("RestartGameCo");
}
}
public IEnumerator RestartGameCo()
{
Thread.Sleep(5000)
yield return new WaitForSeconds(0.5f);
SceneManager.LoadScene("Level1");
}
Related
I'm writing an Asteroid clone and I am running up against an issue with re-spawning my player object after it is deactivated on collision with an asteroid. I run the SetActive(false) function to turn off the object from within the player object and when I try to use SetActive(true) from the GameManager to reactivate the Player object it doesn't work. Any help would be appreciated.
Here is the section of code in the Player script where I turn off the Player object:
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Asteroid")
{
_rigidbody.velocity = Vector3.zero;
_rigidbody.angularVelocity = 0.0f;
this.gameObject.SetActive(false);
GameManager.Instance.PlayerDeath();
}
and here is the section of the GameManager where I try to reactivate the Player object:
public void PlayerDeath()
{
this.lives--;
if (this.lives <= 0)
{
GameOver();
}
else
{
//Invoke(nameof(Respawn), this.respawnTimer);
StartCoroutine(Respawn(respawnTimer));
}
}
public IEnumerator Respawn(float f)
{
yield return new WaitForSeconds(f);
this.player.transform.position = Vector3.zero;
this.player.gameObject.SetActive(true);
}
Is your GameOver method will call IEnumerator Respawn?
if not then I think that's your problem, cause what I know so far is that
an if statement will executed and if it's True if not True then it will be an Else statement, in your case your IEnumerator was inside Else statement try to move it above.
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.layer == layer)
{
StopAllCoroutines();
Destroy(gameObject, 1f);
}
}
//Coroutine is called from another script
public IEnumerator MoveRb(Vector3 destination)
{
yield return new WaitUntil(() => rb.velocity == Vector3.zero);
destination.y = rb.position.y;
rb.velocity = transform.right;
//this is where i get an error
yield return new WaitUntil(() => Vector3.Distance(rb.position, destination) < 0.1f);
rb.velocity = Vector3.zero;
}
Basically, getting "MissingReferenceException" when trying to destroy an object while running coroutine. Delay of 1 second, nor replacing "WaitUntil" with while loop and "yield return null" doesn't fix this issue. The only place where object gets destroyed is inside of "OnCollisionEnter" inside of the same gameObject script. What am I missing here?
Full exception message:
MissingReferenceException: The object of type 'Rigidbody' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
As you posted on the other answer you actually run this routine from another script
public class ConveyourBelt : MonoBehaviour
{
void OnCollisionEnter(Collision other)
{
if (other.gameObject.TryGetComponent(out MovingPart movingPart))
{
// runs this Coroutine on THIS component
StartCoroutine(movingPart.MoveRb(transform.position + transform.right));
}
}
}
The issue here is that this ConveyourBelt component is running the Coroutine, not the MovingPart component attached to the other object
=> The call to
StopAllCoroutines();
in the MovingPart component has no effect at all since it is never running that routine!
So when you destroy the object after 1 second the routine could still be running on the ConveyourBelt component.
As a solution you should rather make the routine running on the other component like
public class ConveyourBelt : MonoBehaviour
{
void OnCollisionEnter(Collision other)
{
if (other.gameObject.TryGetComponent<MovingPart>(out var movingPart))
{
// rather runs the Coroutine on the movingPart component
movingPart.StartCoroutine(movingPart.MoveRb(transform.position + transform.right));
}
}
}
The code should work in theory. I think the problem is you are trying to access the coroutine again during this 1 second destroy delay. This results in the coroutine running while the gameobject is destroyed.
If you want this 1s delay then either make sure that the coroutine can't get called again after you have collided with something or the easier way is to make the coroutine error proof. Means you check if the rigidbody is null before you use it.
I kinda stuck with my script right now. When a gameObject, with my script attached to it, has a trigger event with a specific gameObject, I want to destroy the specific gameObject after an amount of time.
So i came to this:
void OnTriggerEnter ( Collider other) {
if (other.gameObject.tag == "leaf1"){
StartCoroutine (LeafDestruction ());
}
}
IEnumerator LeafDestruction(){
yield return new WaitForSeconds (5);
Destroy (gameObject);
}
I know it's a noob mistake but i think i miss something, because when i run this script, it destroys the gameObject with the script attached to it, and not the specific gameObject(with tags).
How can i fix that?
A simpler solution is to use the optional 2nd parameter of the Destroy method:
The object obj is destroyed immediately after the current Update loop, or t seconds from now if a time is specified.
Given the official parameters:
Parameters
obj
The object to destroy.
t
The optional amount of time to delay before destroying the object.
You can adjust your if statement to:
if (other.gameObject.tag == "leaf1")
Destroy(other.gameObject, 5.0f);
Basically you need to tell your coroutine that it should destroy the other.gameObject and not the gameObject that is running this script.
So what you could do is add a parameter to your coroutine, passing in the gameObject that it should really be destroyed:
void OnTriggerEnter ( Collider other) {
if (other.gameObject.tag == "leaf1")
{
IEnumerator coroutine = LeafDestruction(other.gameObject);
StartCoroutine (coroutine);
}
}
IEnumerator LeafDestruction(GameObject toDestroy){
yield return new WaitForSeconds (5);
Destroy (toDestroy);
}
You destroyed the object instead of the leaf. The gameObject is an alias of this.gameObject, which is the game object this script component is attached to. Note MonoBehaviour inherits from Behaviour and Behaviour inherits from Component.
GameObject leafObject;
void OnTriggerEnter ( Collider other) {
if (other.gameObject.tag == "leaf1"){
leafObject = other.gameObject;
StartCoroutine (LeafDestruction ());
}
}
IEnumerator LeafDestruction(){
yield return new WaitForSeconds (5);
Destroy (leafObject);
}
I have a BonusController script as a component of a Bonus gameObject. This Bonus must be destroyed on collision but has to "act" for a few seconds. I have started a coroutine for this, but the script stops its execution (I think it's because I set the gameObject inactive). Here is the code of BonusController:
void OnTriggerEnter2D(Collider2D col)
{
StartCoroutine(speedUp(1));
gameObject.SetActive(false);
}
IEnumerator speedUp(float seconds)
{
Debug.Log("before");
yield return new WaitForSeconds(seconds);
Debug.Log("after"); // <--- This is never called
}
How can I remove the object and don't stop the coroutine script execution?
Can't you just disable the mesh renderer and collider? This way the gameobject will still exists, but the user won't be able to see it.
You cannot pull the ground on which you are standing. :)
Just disable the SpriteRenderer as you are using 2D methods. And keep the object alive and enable.
{
StartCoroutine(speedUp(1));
//gameObject.SetActive (false);
GetComponent<SpriteRenderer> ().enabled = false;
GetComponent<Collider2D> ().enabled = false;
// Above line will deactivate the first collider it will find.
}
IEnumerator speedUp(float seconds)
{
Debug.Log("before");
yield return new WaitForSeconds(seconds);
Debug.Log("after"); // <--- This is never called
}
In order to destroy the object after some time, call destroy inside the coroutine after the yield.
yield return WaitForSeconds(second);
Destroy(this);
Now it is properly destroyed and will free up memory opposed to still being there, invisible, but taking up resources.
I m new to unity and c# as well. I want to delay a function hence using WaitForSeconds() but my problem is that the statement after WaitForSeconds() is not executed and hence there is no delay shown.
Below is the code:
public void GameOver(){
StartCoroutine (Load ());
Debug.Log("loadDelay");
}
IEnumerator Load(){
Debug.Log ("enum");
yield return new WaitForSeconds(3);
Debug.Log("waited");
}
the output on console shows:
enum
loadDelay
At the same time without any delay when the GameOver() is called and Debug.Log("waited");
is not executed at all. I really don't understand the problem. Please explain if I'm doing something wrong.
Thanks
Code where GameOver() is called:
public class Collision : MonoBehaviour {
public GameObject explosion;
BgScroll bg;
void Start () {
bg = GetComponent<BgScroll> ();
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D(Collision2D col){
if (col.gameObject.tag == "Enemy") {
//Debug.Log("destroyed");
explosion.renderer.sortingLayerName = "foreground";
Instantiate (explosion, transform.position, Quaternion.identity);
Destroy (gameObject);
Debug.Log("destroyed");
bg.GameOver();
}
}
}
According to Unity's Documentation for WaitForSeconds you are doing it right. and your code works good when i tried to run it. There must be some other problem, or you might have other logs before "waited". I got output in console window as
enum
loadDelay
waited
and obviously "waited" will be printed after 3 seconds if your player is running at that time.