[SOLVED] Error when trying to play WebGL build of Number Wizard

I receive this error when i try to play the game

This is consistent with every website i’ve used to try and upload my game: Gamebucket, itch.io and sharemygame

More information available upon request, however i’m not sure how to find the error log to copy and paste the whole error

Not a whole lot to go on there really…

Which version of Unity are you using?

Sorry, i had a lot more in an earlier post but for some reason it was stuck on 100% Uploading?

I’m using Unity 5.6 to upload it, the original version of the file was 4.6, the upload was fine, i did one change to the code because of it being obsolete,

my problem is that i have two buttons at the start, both being managed by Level Manager,
one works fine while the other doesn’t.



1 Like

Hi,

No worries.

Could you upload the complete project files here (assuming it’s under 10mb) and I will take a look for you.

1 Like

Hi,

Thank you yes, this is what I was after.

I have a copy of Unity 5.6.2 which opened the project, despite the warning about being created in 5.6.3.

image


Reproduction;

I have created a WebGL build and uploaded to ShareMyGame.com and can confirm the following behaviour;

  • Clicking “Click Me!” loads the correct scene.
  • Clicking “Return to Menu” loads the correct scene.
  • Clicking on “Start” throws the error you have described.

Observations;

Having looked at the project, I note the following things;

  • LevelManager.cs references the Application.Load() method which is obsolete;

    using UnityEngine;
    
    public class LevelManager : MonoBehaviour {
    
        public void LoadLevel(string name){
            Debug.Log ("Level load requested for: " + name);
            #pragma warning disable CS0618 // Type or member is obsolete
                Application.LoadLevel(name);
            #pragma warning restore CS0618 // Type or member is obsolete
        }
    
    }
    

    This method should be replaced with SceneManager.LoadScene. It supports the same method signatures, thus, passing in the name of the scene you want to load will work.

    You do however need to add the using UnityEngine.SceneManagement; directive at the top of the script.

  • NumberWizard.cs also references to obsolete Application.Load() method within the NextGuess() method;

    void NextGuess() {
    	
        guess = Random.Range(min,max);
        text.text = guess.ToString();
        maxGuessesAllowed = maxGuessesAllowed - 1;
        if(maxGuessesAllowed <= 0){
            #pragma warning disable CS0618 // Type or member is obsolete
                Application.LoadLevel("Win");
            #pragma warning restore CS0618 // Type or member is obsolete
        }
    }
    
  • In the Game scene, the EventSystem GameObject in the Hierarchy has two Level Manager script components which it doesn’t need.

    These components should be removed.

    image

  • In the Game scene, the LevelManager GameObject in the Hierarchy has a Number Wizard script component which it doesn’t need.

    This should be removed.

    image


Resolution:

By removing the Number Wizard script component from the LevelManager GameObject, within the Hierarchy, in the Game scene, I was able to build a WebGL build and publish it to ShareMyGame.com, where it ran as intended.

After confirming that this worked, I also removed the two Level Manager script components from the EventSystem GameObject, within the Hierarchy, in the Game scene. I also replaced the obsolete method references to use the new SceneManager.

I hope this is of use :slight_smile:

1 Like

Now i have a new problem where my game isn’t functioning anymore, the guesses aren’t working and the buttons during the game scene aren’t working either.

Did i delete too much out of my code?

Hi @Dhanee,

Time to debug your code then!


Debug;

If you disable the Maximize on Play button in the Game view;

…and then run the game, then click on the Console view, this will give you an insight as to what the problem may be;

image

The NullReferenceException error points to line 31 in NumberWizard.cs, if we take a look at that specific line;

image

On this line we have a couple of possibilities… we are trying to set the text property of a UI Text object called Text and we are trying to use the ToString() method on guess.

If there had been an issue with the population of guess on the line above using Random.Range() we would expect to see an error pointing to that line, as we didn’t it would be fair at this stage to assume it is an issue with the UI Text object.

A NullReferenceException is generated when an object is referenced and doesn’t exist, so in this case we could assume that our UI Text object called text doesn’t exist at this stage in the code… Lets have a look and see if this is the case…

If we look at the top of the NumberWizard class we can see that you declare a public variable of type Text, called text, but then following the code from that point to our error, at no point do we instantiate it in any way. Thus, we are expecting it to already exist in our code, if it isn’t, then we haven’t passed it in!

We need to locate where we are referencing the UI Text object and passing it to the NumberWizard script, we know the error is caused when the Game scene is triggered, so lets start by opening the Game scene.

Looking at the objects in the Hierarchy, I found that your LevelManager GameObject has in fact got the NumberWizard script attached.

image

Selecting it in the Hierarchy and then viewing it in the Inspector reveals that the exposed variable called Text is not actually populated;

image

The resolution here would be to drag the relevant UI Text object from the Hierarchy into the exposed Text variable on the Number Wizard script, in this case, this would be the GameObject named Guess.

image

Having done so, this would now be the time to run the game again and check to see if this has made a difference.


Whilst testing at this end I can confirm that this does resolve the issue for the Higher and Lower buttons. However, I did note that your Correct button is not behaving as it should.

See if you can follow the same approach above and determine what the issue could be, what is happening and why? Then try to resolve it and re-test.


On a minor note - you have your NumberWizard script attached to a GameObject named LevelManager, whilst this will work, it obviously can be a little confusing, especially when you have a script called LevelManager. Once you have resolved the issues above, it would be worth considering having one GameObject named LevelManager with the LevelManager script attached to it, and another GameObject called NumberWizard and attach the NumberWizard script to it. I think you will find this a little easier going forward - as will those that help you :slight_smile:

Hope this helps, and let me know how you get on resolving the Correct button issue.

1 Like

Hi! I did some messing around with the Heirarchy on a new set of code and after a little messing around i got it to work! Thanks for the help :slight_smile:
I don’t really know TOO specifically what i did to fix it, but it’s up and running and i appreciate the help you’ve given!

1 Like

Hi @Dhanee,

I am glad you have things working :slight_smile:

Might be worth running your eyes over the project and making sure you are familiar with it before moving on.

Enjoy the rest of the course and I look forward to seeing what you share :slight_smile: