- Home /
Code doesn't work in WebGL build
Hi,
I have a script that perfectly works in the Unity Editor, however once I build it in WebGL, it does not work. What also is worth mentioning, is that my Debug.Log statements from this script are not being printed in the console.
The script is moving the object by a specific distance in "jump movements" until it reaches a breakpoint and then moves it back to the centre. Once the game object is back, the break point should increase and therefore the game object should go further out. However, in WebGL it jumps out once and then goes back and does not increase the breakpoint. I have a similar movement, that is fluent, instead of jumping and that works fine both in the editor and the build.
Here's my code:
protected void CheckForChangeDirection(bool condition){
if(condition){
goingBack = false;
direction = -direction;
}
else if(Mathf.Abs(transform.position.x) >= Mathf.Abs(currentBreakpoint) && !goingBack){
goingBack = true;
direction = -direction;
NewBreakPoint();
}
}
protected void NewBreakPoint(){
currentBreakpoint += increaseDistance;
posneg += new Vector2(increaseDistance, 0);
}
private IEnumerator Jump(){
yield return new WaitForSeconds(0.75f);
Move();
CheckForChangeDirection(transform.position.x * posneg.x * 10 <= 1);
StartCoroutine("Jump");
}
protected override void Move(){
transform.position = (Vector2)transform.position + speed*direction;
}
Please, please help, I have no idea why this is happening. And have no idea how to debug it, as it works in the editor and doesn't print the Debug.Log statements in the browsers console...
Answer by Bunny83 · Nov 13, 2019 at 12:12 PM
I can't see anything obvious. However if the problem is really part of those snippets it's most likely your StartCoroutine call with a string argument. There really is no reason to use that, at all.
We don't see where you initially start the coroutine, but you really should use
StartCoroutine(Jump());
Constantly restarting the coroutine every 0.75 seconds is a bad idea. You will generate tons of garbage that way. Just keep the coroutine running:
private IEnumerator Jump()
{
while(true)
{
yield return new WaitForSeconds(0.75f);
Move();
CheckForChangeDirection(transform.position.x * posneg.x * 10 <= 1);
}
}
This will have the same effect but does not constantly terminate and restart the coroutine.
Thank you for the answer! I will see if it helps.
Now I'm getting the Debug.Log output in the console, so I can debug it, thank you!
Answer by unity_eUpT1lvqDGObtw · Nov 14, 2019 at 10:43 AM
It turned out that the problem was with the condition: transform.position.x posneg.x 10 <= 1. The value was swapping between 0 and 1 and in the editor 1 <= 1 was returning false, while in the browser it was correctly true. Fixed by changing to transform.position.x posneg.x 10 < 1.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How to use Webgl Debug Symbols? 0 Answers
WebGL cross-domain error when loading .mem file from remote 1 Answer
WebGl build keeps failing 0 Answers