I know I can use Wait on a C# script by creating an instance and then using ExecuteAsync(). However, I would like to use it on an custom command. If i call the command inside the ExecuteAsync method of my custom command, I cannot use it properly. The script does not have any errors and yet Wait seems to be ignored. Here's the code:
Code: Select all
using Naninovel;
using Naninovel.Commands;
using UniRx.Async;
using UnityEngine;
[CommandAlias("disableObject")]
public class DisableObject : Command
{
public StringParameter objectName;
GameObject gameObject;
Collider collider;
Animator animator;
Wait waitCommand;
public StringParameter waitTime;
public override UniTask ExecuteAsync(CancellationToken cancellationToken = default)
{
gameObject = GameObject.Find(objectName);
animator = gameObject.GetComponent<Animator>();
animator.Play("FadeObject");
waitCommand = new Wait();
waitCommand.WaitMode = waitTime;
await waitCommand.ExecuteAsync();
collider = gameObject.GetComponent<Collider>();
collider.enabled = false;
return UniTask.CompletedTask;
}
}
In line 21, the program throws a warning:
warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
However, putting await doesn't work either because await can only be used in a asynchronous method. I then tried to create all the Wait logic on a separate function called Wait() and set it as async
Code: Select all
public override UniTask ExecuteAsync(CancellationToken cancellationToken = default)
{
gameObject = GameObject.Find(objectName);
animator = gameObject.GetComponent<Animator>();
animator.Play("FadeObject");
WaitFunction();
collider = gameObject.GetComponent<Collider>();
collider.enabled = false;
return UniTask.CompletedTask;
}
async void WaitFunction()
{
waitCommand = new Wait();
waitCommand.WaitMode = waitTime;
await waitCommand.ExecuteAsync();
}
The object is deactivated but there is no wait time. Any tips?