[Question]WebGL Build Won't Change Scenes / Works fine in Unity

Hey everyone, one of my students is experiencing a strange bug. When in Unity, you can complete a level and a proper scene change occurs. However, once built for WebGL and uploaded to itch.io, the Scene change does not occur any longer when all of the bricks are destroyed. No exceptions are thrown. Here’s a link to her game: https://cinn-senpai.itch.io/ephem-knight

And the relevant scripts:

Brick:

public AudioClip crack;
public Sprite[] hitSprites;
public static int breakableCount = 0;

private int timesHit;
private LevelManager levelManager;
private bool isBreakable;


// Use this for initialization
void Start () {
isBreakable = (this.tag == "Breakable");
    if (isBreakable)
    {
        breakableCount++;
        
    }
    print(breakableCount);

    timesHit = 0;
    levelManager = GameObject.FindObjectOfType<LevelManager>();
}

void OnCollisionEnter2D (Collision2D col)
{
    AudioSource.PlayClipAtPoint(crack, transform.position);
    if (isBreakable)
    {
        HandleHits();
    } 
}

void HandleHits()
{
    int maxHit = hitSprites.Length + 1;
    timesHit++;
    if (timesHit >= maxHit)
    {
        breakableCount--;
        levelManager.brickDestroyed();
        Destroy(gameObject);
    }
    else
    {
        LoadSprites();
    }

}

void LoadSprites()
{
    int spriteIndex = timesHit - 1;
    if (hitSprites[spriteIndex])
    {
        this.GetComponent<SpriteRenderer>().sprite = hitSprites[spriteIndex];
    }
}

void SimulateWin()
{
    levelManager.LoadNextLevel();
}

LevelManager:

public LevelManager levelManager;
private Ball ball;
private Vector3 balpos;
private Vector2 balVec;

private void Start (){

     ball = GameObject.FindObjectOfType<Ball>();
     if (ball)
    {
        balpos = ball.transform.position;
        balVec = ball.GetComponent<Rigidbody2D>().velocity;
    }
}

public void ResetBall()
{
    Debug.Log("Ball Reset");
    balpos = ball.transform.position;
    balVec = ball.GetComponent<Rigidbody2D>().velocity;
    ball.Loselife();
}

public void LoadLevel(string name)
{
    SceneManager.LoadScene(name);
}

public void LoadNextLevel ()
{
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
public void brickDestroyed()
{
    if (Brick.breakableCount <= 0)
    {
        LoadNextLevel();
    }
}

LoseCollider:

private LevelManager levelManager;
public int lives = 3;
public Text text;


void Start()
{
    levelManager = FindObjectOfType<LevelManager>();
}
 
// when ball hits lose collider goes to game over
void OnTriggerEnter2D(Collider2D trigger)
{
    lives--;
    Debug.Log("Life lost");
    text.text = lives.ToString();
    if (lives <= 0)
    {
        lives = 3;
        levelManager.LoadLevel("Lose");
    }
    else
    {
        levelManager.ResetBall();
    }
        
   
}

Build Settings:

I completed the first level. It changed. The blocks were in different locations. But then auto-play kicked in and I couldn’t do anything.

The lives were also set back to 3.

After the AI had completed that level, the next level was loaded. I had control again at that point. I let the ball go out of bounds until I was out of lives. The You Lose screen appeared.

It would seem it is loading levels, but there are some quirks.

1 Like

Maybe it’s just me. For some reason I can clear all the blocks but still not get a transition… I had 1 life left.

Probably a daft question but are you definitely on the remote hosted version of the game? e.g. you don’t have any local host headers configured to point to a local server/machine instead which may be hosting an older version?

Assuming not, try clearing the temporary internet files / cache etc.

I’m not entirely sure how to check. I rebuilt the game after making some small changes and adjusting Mathf.Clamp and disabling autoplay. It works fine locally. I am having her upload that build to itch.io and we will see.

hmm… ok, seems odd that it would play for me though? (bar the auto-play quirk)

Let me know if you want me to try a different link.

Add this line to both LoadNextLevel() and LoadLevel() in the LevelManager.cs:

You’re not resetting the static variable at every scene change, so it just keeps growing more and more.

1 Like

Thanks Galandil!

You’re welcome.

There’s some additional learning here btw, specifically why the use of global state variables is almost always a bad idea. :smiley:

Privacy & Terms