{"record":{"id":"cc492649953b980e","repo":"Cysharp/UniTask","slug":"delay-does-not-allow-minus-delaytimespan-delaytim","errorCode":null,"errorMessage":"Delay does not allow minus delayTimeSpan. delayTimeSpan:","messagePattern":"Delay does not allow minus delayTimeSpan\\. delayTimeSpan:","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Delay.cs","lineNumber":169,"sourceCode":"        }\n\n        public static UniTask Delay(TimeSpan delayTimeSpan, bool ignoreTimeScale = false, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false)\n        {\n            var delayType = ignoreTimeScale ? DelayType.UnscaledDeltaTime : DelayType.DeltaTime;\n            return Delay(delayTimeSpan, delayType, delayTiming, cancellationToken, cancelImmediately);\n        }\n\n        public static UniTask Delay(int millisecondsDelay, DelayType delayType, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false)\n        {\n            var delayTimeSpan = TimeSpan.FromMilliseconds(millisecondsDelay);\n            return Delay(delayTimeSpan, delayType, delayTiming, cancellationToken, cancelImmediately);\n        }\n\n        public static UniTask Delay(TimeSpan delayTimeSpan, DelayType delayType, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false)\n        {\n            if (delayTimeSpan < TimeSpan.Zero)\n            {\n                throw new ArgumentOutOfRangeException(\"Delay does not allow minus delayTimeSpan. delayTimeSpan:\" + delayTimeSpan);\n            }\n\n#if UNITY_EDITOR\n            // force use Realtime.\n            if (PlayerLoopHelper.IsMainThread && !UnityEditor.EditorApplication.isPlaying)\n            {\n                delayType = DelayType.Realtime;\n            }\n#endif\n\n            switch (delayType)\n            {\n                case DelayType.UnscaledDeltaTime:\n                    {\n                        return new UniTask(DelayIgnoreTimeScalePromise.Create(delayTimeSpan, delayTiming, cancellationToken, cancelImmediately, out var token), token);\n                    }\n                case DelayType.Realtime:\n                    {","sourceCodeStart":151,"sourceCodeEnd":187,"githubUrl":"https://github.com/Cysharp/UniTask/blob/ceac8d6946b1125fe782cd171fbcb245b567dbf9/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Delay.cs#L151-L187","documentation":"Thrown by UniTask.Delay(TimeSpan) when delayTimeSpan is negative. UniTask delays accumulate elapsed time against the target duration; a negative duration cannot elapse. The guard catches sign errors early, before the promise is created.","triggerScenarios":"Calling UniTask.Delay(TimeSpan.FromMilliseconds(-100)), or passing a TimeSpan computed from a subtraction that yields a negative result (e.g., deadline - now when the deadline has already passed).","commonSituations":"Timeout calculations where the deadline has already passed: TimeSpan.FromSeconds(targetTime - Time.time). Deserialization or configuration that produces negative durations. Clock skew in network time calculations.","solutions":["Clamp the TimeSpan to zero: delayTimeSpan < TimeSpan.Zero ? TimeSpan.Zero : delayTimeSpan","Check if the deadline has already passed and skip the delay entirely","Use TimeSpan.Max(TimeSpan.Zero, computedSpan) (C# 8.0+ for TimeSpan supports CompareTo, otherwise use a ternary)"],"exampleFix":"// before\nvar wait = deadline - DateTime.UtcNow; // could be negative\nawait UniTask.Delay(wait);\n\n// after\nvar wait = deadline - DateTime.UtcNow;\nif (wait > TimeSpan.Zero)\n    await UniTask.Delay(wait);","handlingStrategy":"validation","validationCode":"TimeSpan safeDelay = delayTimeSpan < TimeSpan.Zero ? TimeSpan.Zero : delayTimeSpan;\nawait UniTask.Delay(safeDelay);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check if a deadline has already passed before computing the delay","Clamp TimeSpan values to TimeSpan.Zero minimum","Log computed delay values during development to catch sign errors"],"tags":["unitask","delay","argument-validation","timespan","unity","csharp"],"backgroundTag":null,"analyzedSha":"ceac8d6946b1125fe782cd171fbcb245b567dbf9","analyzedAt":"2026-08-13T19:16:39.025Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}