- Home /
The question is answered, right answer was accepted
If i use async function, during the await Task.delay() , It starts another task
I am using a simple function without any update as given
public async void ObjSupport_PositionArrivedAsync(string Coordinate, double Value)
{
turret2carriage = GameObject.Find("Turret2Carriage");
float ivalue = (float)Value;
switch (Coordinate)
{
case "Z+":
{
Vector3 turret2position =
turret2carriage.transform.localPosition;
float turret2currentXpos = turret2position.x;
turret2position.x = Mathf.Clamp(turret2position.x + ivalue, -1200f, 500f);
while (turret2currentXpos <= turret2position.x)
{
await Task.Delay(100);
turret2currentXpos += 10f;
Vector3 posX =
new Vector3(turret2currentXpos, turret2position.y, turret2position.z);
turret2carriage.transform.localPosition = posX;
}
}
break;
case "Z-":
{
Vector3 turret2position = turret2carriage.transform.localPosition;
float turret2currentXpos = turret2position.x;
turret2position.x = Mathf.Clamp(turret2position.x - ivalue, -1200f, 500f);
while (turret2currentXpos >= turret2position.x)
{
await Task.Delay(100);
turret2currentXpos -= 10f;
Vector3 posX =
new Vector3(turret2currentXpos, turret2position.y, turret2position.z);
turret2carriage.transform.localPosition = posX;
}
}
break;
}
}
I am calling this function from another script . from where I pass Z+ or Z- with value.But during await Task.Delay(100);
it go back and starts executing another call. And continue them like that. What I want is that during the await Task.Delay(100);
the execution should stay there ,It should not goback .
Can you show how you are calling this function from another script?
Yes I am calling it from here. I have list of codes from user and I am calling them this way, It is long code which i have shortened for only Z. Now if i only give one value of Z for example Z100 (i-e positive) it will call arr.ObjSupport_PositionArrivedAsync("Z+", Port.ZValue - Port.CurrentPositionZ);
And if I give 1 value of Z-100 (i-e negative) it will call arr.ObjSupport_PositionArrivedAsync("Z-", Port.CurrentPositionZ - Port.ZValue);
Now Problem is that when i give two values for example Z100 Z-300 .So according to me it should first call Z+ part complete its execution and then Z- part .But when await Task.Delay(100); is called mentioned in above code , during the case Z+ , the compiler comeback and start check and start executing the Z- $$anonymous$$us and swaps between Z+ and Z- case during Delay
if (lG$$anonymous$$Code.StartsWith("Z"))
{
Port.ZValue = Convert.ToDouble(lG$$anonymous$$Code.Substring(1, lG$$anonymous$$Code.Length - 1));
if (Port.CurrentPositionZ > Port.ZValue)
{
arr.ObjSupport_PositionArrivedAsync("Z-", Port.CurrentPositionZ - Port.ZValue);
}
else
{
arr.ObjSupport_PositionArrivedAsync("Z+", Port.ZValue - Port.CurrentPositionZ);
}
Port.CurrentPositionZ = Port.ZValue;
}
Am i using it correct? I Also tried Coroutines but result is same. That when it reaches delay it goes back to call function. Kindly guide me?
Answer by diego-giacomelli · Nov 02, 2020 at 03:13 PM
When you call an async method will need to use the await
keyword:
await arr.ObjSupport_PositionArrivedAsync("Z-", Port.CurrentPositionZ - Port.ZValue);
More at https://docs.microsoft.com/en-us/dotnet/csharp/async
Thanks Alot! I have no words to say to you...Thanks once again.... I was missing that small point
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to translate code with yield waitforseconds to c#? 1 Answer
Create a delay between function executions 1 Answer
Wait for seconds question 1 Answer