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): using UnityEngine; using System.Collections; public class DoorSlide : MonoBehaviour { void Start() { StartCoroutine(Sleep()); } IEnumerator Sleep() { yield return new WaitForSeconds(3); // Move the object upward in world space 1 unit/second. transform.Translate(Vector3.up * Time.deltaTime, Space.World); } }
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?
Then it should be something like this: Code (CSharp): [SerializeField] private float _moveTimer = 5f; IEnumerator SleepMove() { yield return new WaitForSeconds(3); float timer = _moveTimer; while (timer > 0) { // Move the object upward in world space 1 unit/second. transform.Translate(Vector3.up * Time.deltaTime, Space.World); timer -= Time.deltaTime; yield return null; } }
Nevermind, figured it out earlier, but her's my solution for anyone who wants it Code (CSharp): using UnityEngine; using System.Collections; public class DoorSlide : MonoBehaviour { void Start() { StartCoroutine(waiter()); } IEnumerator waiter() { yield return new WaitForSeconds(5); for (int i = 0; i < 1000; i++) { yield return new WaitForSeconds(0.05f); // Move the object upward in world space 1 unit/second. transform.Translate(Vector3.up * Time.deltaTime, Space.World); } } }
That code is just terrible in so many ways and will cause nothing but problem. What was wrong with @xVergilx's solution for you?