0

After a couple of tries platform doesn't to its original position, but then it gets deleted and I don't know how to fix it.

public class FallingPlatform : MonoBehaviour
{
    private Rigidbody2D rb;

    // Use this for initialization
    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    private void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.name.Equals("Ellen"))
        {

            Invoke("DropPlatform", 0.5f);
            Destroy(gameObject, 2f);
            InvokeRepeating("DropPlatform", 0.5F);
            respawn(gameObject, 2f);
        }
    }

    private void InvokeRepeating(string v1, float v2)
    {
        throw new NotImplementedException();
    }

    private void respawn(GameObject gameObject, float v)
    {
        throw new NotImplementedException();
    }

    private void DropPlatform()
    {
        rb.isKinematic = false;
    }
JeremyW
  • 5,157
  • 6
  • 29
  • 30

1 Answers1

0

Some context around what you are trying to do is missing. But, I am guessing that you are making a game where you have a player jumping on the different platforms and they fall after it touches them and then you want them to reappear. A few things to note: 1) Your respawn function is just throwing an exception(so there is no point in calling it). 2) Once the Destroy function runs, nothing else will run(this is probably why it stops working after a couple of times.

There are probably better approaches to achieve what you are trying to do (without setting rb.isKinematic to false to enable gravity, for example). But in order to answer your question without changing your code too much, you can try the code below. It should make the platform drop after 0.5 seconds and make it get back to where it was 1.5 seconds later. If this is not what you are looking for, please share more information on what you are trying to do.

public class FallingPlatform : MonoBehaviour
{
    private Rigidbody2D rb;
    private Transform originalTransform;

    // Use this for initialization
    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    private void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.name.Equals("Ellen"))
        {

            Invoke("DropPlatform", 0.5f);
            respawn(gameObject, 2f);
        }
    }

    private void InvokeRepeating(string v1, float v2)
    {
        throw new NotImplementedException();
    }

    private void respawn(GameObject gameObject, float v)
    {
        gameobject.transform.position = originalTransform.position
        rb.isKinematic = true;
    }

    private void DropPlatform()
    {
        originalTransform = gameobject.transform;
        rb.isKinematic = false;
    }