Search Unity

Sleep period

Discussion in 'Scripting' started by iChocolqteMilk, Feb 20, 2019.

  1. iChocolqteMilk

    iChocolqteMilk

    Joined:
    Feb 20, 2019
    Posts:
    4
    I want to add a sleep period before moving an object upward, however my code currently doesn't work. Please help!!! (btw i basically understand nothing so an example code would be great) (also currently don't get errors, just doesn't work)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DoorSlide : MonoBehaviour
    5. {
    6.      void Start()
    7.     {
    8.         StartCoroutine(Sleep());
    9.     }
    10.     IEnumerator Sleep()
    11.     {
    12.         yield return new WaitForSeconds(3);
    13.         // Move the object upward in world space 1 unit/second.
    14.         transform.Translate(Vector3.up * Time.deltaTime, Space.World);
    15.     }
    16. }
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    The code on line 14 is only executed for one frame and then the coroutine finishes. How long or far do you need the object to move for?
     
  3. iChocolqteMilk

    iChocolqteMilk

    Joined:
    Feb 20, 2019
    Posts:
    4
    a
    about 5 secs or so
     
    charlesfelix442 likes this.
  4. iChocolqteMilk

    iChocolqteMilk

    Joined:
    Feb 20, 2019
    Posts:
    4
    I also tried using if/else with a variable counter but that didn't work
     
  5. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Then it should be something like this:
    Code (CSharp):
    1. [SerializeField]
    2. private float _moveTimer = 5f;
    3.  
    4. IEnumerator SleepMove()
    5.     {
    6.         yield return new WaitForSeconds(3);
    7.  
    8.         float timer = _moveTimer;
    9.         while (timer > 0) {
    10.             // Move the object upward in world space 1 unit/second.
    11.             transform.Translate(Vector3.up * Time.deltaTime, Space.World);
    12.  
    13.             timer -= Time.deltaTime;
    14.             yield return null;
    15.         }
    16.     }
     
  6. iChocolqteMilk

    iChocolqteMilk

    Joined:
    Feb 20, 2019
    Posts:
    4
    Nevermind, figured it out earlier, but her's my solution for anyone who wants it
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DoorSlide : MonoBehaviour
    5. {
    6.  
    7.     void Start()
    8.     {
    9.         StartCoroutine(waiter());
    10.     }
    11.     IEnumerator waiter()
    12.     {
    13.         yield return new WaitForSeconds(5);
    14.  
    15.         for (int i = 0; i < 1000; i++)
    16.         {
    17.             yield return new WaitForSeconds(0.05f);
    18.             // Move the object upward in world space 1 unit/second.
    19.             transform.Translate(Vector3.up * Time.deltaTime, Space.World);
    20.         }
    21.     }
    22. }
     
  7. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    That code is just terrible in so many ways and will cause nothing but problem.

    What was wrong with @xVergilx's solution for you?
     
    CoCoNutti likes this.
  8. charlesfelix442

    charlesfelix442

    Joined:
    May 25, 2019
    Posts:
    9
    What is the solution to the problem?
     
  9. charlesfelix442

    charlesfelix442

    Joined:
    May 25, 2019
    Posts:
    9
    The counter did not work correctly.
     
  10. charlesfelix442

    charlesfelix442

    Joined:
    May 25, 2019
    Posts:
    9
    Thanks for the answer.
     
  11. charlesfelix442

    charlesfelix442

    Joined:
    May 25, 2019
    Posts:
    9
    I think this is one of the best example code.