Search code examples
c#visual-studiounity-game-enginegame-engine

How can I make the StartCoroutine working in unity?


I'm new in c#, and I'm trying to make a platform falling down when the player stand on it. I used StartCoroutine so the platform fall down after five seconds, but for some reason my couroutine doesn't working.

this is the code:

public GameObject player;




   private Rigidbody rb;

    void Start()
    {
       rb = GetComponent<Rigidbody>();

       rb.useGravity = false;

        //rb.isKinematic = false;
    }

    // Update is called once per frame
    void Update()
    {

    }


    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "player")

        {

            StartCoroutine(ObjectFall());

        }
    }

    IEnumerator ObjectFall()
    {

        yield return new WaitForSeconds(5f);
        Debug.Log("Its working");


        this.rb.useGravity = true;

        //this.rb.isKinematic = true;
    }



}

Solution

  • You dont need to use IEnumerable if you have no parameters for the function. You could use Invoke("ObjectFall", 5) and turn your IEnumerable to void without yield. Its probably not the source of the problem but give it a try. Scale in your game is probably wrong that's why mass 1000 works. Try fixing your scale for the whole scene. Remember 1 unit = 1 meter more or less