0

I am using await Task.Delay(10); in unity C# for delay .After build and run in Web-GL format .The execution stops at this line that has delay .When I remove the delay it executes.

while (XAxiscurrentXpos <= -izvalue) 
{ 
    await Task.Delay(10); 

    XAxiscurrentXpos += 0.001f; 
    Vector3 posx = new Vector3(XAxiscurrentXpos, XAxisposition.y, XAxisposition.z); 
    XAxis.transform.localPosition = posx; 
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
aqeel
  • 87
  • 1
  • 13
  • 2
    WebGL doesn't support asynchronous or multithreaded executions out of the Box... => [async - await and WebGL](https://forum.unity.com/threads/async-await-and-webgl-builds.472994/) – derHugo Nov 05 '21 at 07:51
  • 1
    So what is alternative for this – aqeel Nov 05 '21 at 08:03
  • 2
    Alternative for what exactly? If you just want [delay code in Unity](https://stackoverflow.com/questions/30056471/how-to-make-the-script-wait-sleep-in-a-simple-way-in-unity) there are many options like Coroutines etc or show your actual code otherwise ... Also there is [`PlayerSettings.WebGL.threadsSupport`](https://docs.unity3d.com/ScriptReference/PlayerSettings.WebGL-threadsSupport.html) which might be what you want to use but it's experimental and not supported everywhere – derHugo Nov 05 '21 at 08:06
  • Don't update your question and completely change the subject ... stick to your original question and get answers to that specific question .... and if you have a new question open a new one ... – derHugo Nov 09 '21 at 07:13
  • Ok I will open a new one ... Thanks – aqeel Nov 09 '21 at 07:32

2 Answers2

2

I have solution by using UniTask. UniTask Link is here

aqeel
  • 87
  • 1
  • 13
-1

So now knowing what you are actually trying to do instead of your task at all you rather want to use e.g.

public class MoveObject : MonoBehaviour
{
    public Transform XAxis;
    public float desiredUnitsPerSecond;

    // This is called once a frame by Unity
    private void Update()
    {
        if(XAxis.localPosition.x <= -izvalue)
        { 
            XAxis.localPosition += Vector3.right * desiredUnitsPerSecond * Time.deltaTime;
        }
    }
}

this moves your object with the linear speed of desiredUnitsPerSecond in its local right direction as long as it is left of -izvalue.

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Ok . I am not using it in update function. But I will Apply it and Will check the results. Thanks ! – aqeel Nov 07 '21 at 06:03
  • ` bool Xpositive=false; void Update() { if (Xpositive == true) { if (XAxiscurrentXpos <= -izvalue) { while (XAxiscurrentXpos <= -izvalue) {XAxis.transform.localPosition += Vector3.right * 0.01f * Time.deltaTime; } } }` And this bool is changing in below code ` public async Task ObjSupport_PositionArrivedAsync(string Coordinate, double Value){switch (Coordinate){case "X+":{ Xpositive = true; }break;}}` Now when this becomes true the update function do not go inside if block Am i doing it right? – aqeel Nov 09 '21 at 07:10
  • Again .. **why** use `async Task` for this? There is nothing in it that needs to be awaited anyway ... Simply use a normal void method? – derHugo Nov 09 '21 at 07:11
  • I have removed async Task but still the update is not going inside ` if (Xpositive == true)` – aqeel Nov 09 '21 at 07:25