WaitForSeconds doesn't wait if time is too low
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectMoverUp : MonoBehaviour
{
public float ForwardSpeed;
public float maxPos;
public DragObject DO;
public float currentPos;
public bool execute = false;
public Coroutine spawnCoroutine;
public float WaitTime;
private void Start()
{
StartCoroutine(WaitToSync());
DO = GameObject.FindWithTag("Player").GetComponent<DragObject>();
}
IEnumerator WaitToSync()
{
yield return new WaitForSeconds(WaitTime);
Execute();
}
public void Execute()
{
execute = true;
}
void Update()
{
if (execute == true)
{
currentPos = transform.position.y;
if (currentPos <= maxPos)
{
ForwardSpeed = DO.ForwardSpeed;
transform.Translate(Vector3.up * Time.deltaTime * ForwardSpeed * 2, Space.World);
}
}
}
}
So this is my code and the problem is that if WaitTime is lower than 2.3f Execute() is called immediately on one object but on another it still waits for WaitForSeconds and I need both objects to wait
Comment
I don't see any reason why this problem would occur given the information you've provided. Are the objects in the scene when you start the scene or are they instantiated dynamically?
Could you try without a coroutine?
public float ForwardSpeed;
public float maxPos;
public DragObject DO;
public float currentPos;
public Coroutine spawnCoroutine;
public float WaitTime;
private float _startTime;
private void Start()
{
_startTime = Time.time;
DO = GameObject.FindWithTag("Player").GetComponent<DragObject>();
}
void Update()
{
if (Time.time - _startTime > WaitTime)
{
currentPos = transform.position.y;
if (currentPos <= maxPos)
{
ForwardSpeed = DO.ForwardSpeed;
transform.Translate(Vector3.up * Time.deltaTime * ForwardSpeed * 2, Space.World);
}
}
}