Bricks don't break in web build

So block breaker has given me a real headache. I resolved an issue where the ball wouldn’t bounce off the bricks after converting to (the wrong version of) Unity 5. The problem I have now is that, while it plays fine in the Play Mode of the editor, once built to WebGL, the bricks don’t break at all! I guess on the plus side, the ball bounces off the bricks now, but the game is still unplayable in it’s current state. Any advice on what I have done wrong and what I need to do would be greatly appreciated.

Google Drive link to project folder (.ZIP) available here

Expected behaviour https://youtu.be/rY2bMgNVQ7U

Actual behaviour https://youtu.be/VsboKDuDIOU

I don’t think it’s a code issue, as it runs fine in the editor. I’m at a complete loss now to be honest and beginning to get really frustrated with this project, but I don’t want to move on until I figure this out because I might run into similar issues in the future so I want to know how to solve it now!

EDIT: Thanks to @Brent_Giles for the fix. Had to delete the breakable tag and reapply it. Don’t know why it works but after 6 weeks of debugging I no longer care, it works and that’ll do for me!

Hey, @TheAwesomeJames!

So, If I could get some more info, I may be able to help. First, check your console for any warnings or errors during build. Even a warning could point to the problem. Next, try using this function:

public static void Log(string message, Object context = null) {
    #if UNITY_EDITOR
        Debug.Log(message, context);
    #else
        Application.ExternalCall("console.log", message);
    #endif
 }

And call it from within your collision/trigger event. You can hit F12 on your webpage to see console logs. Let me know the results of that. If it isn’t logging, try increasing your collider sizes. If it is… well, let me know, and we’ll go from there. This post in the Unity Forums may help, too.

Hi @Gwynblade

thanks for responding so quickly, sorry I’ve left it a day to get back to you but I only have a small window in which to work at the moment.

This might seem like a silly question, but how would I go about calling that function from within the collision event. Like, what should the code look like to do that?

I was super confident going into this section of the course as I’d gone through the first parts really quickly but that confidence has been shattered and I’ve gotten really bogged down in errors at the end when it’s come time to publish my work. But I’m determined to figure it out, even if that means asking for loads of help and guidance! So I really appreciate you taking the time out to give me a hand on this!

Hey, @TheAwesomeJames!

No worries! I understand that for sure – most days I don’t get much free time, either!

If you put that method in your brick class (I haven’t actually gotten to this part of the course yet, so I’m assuming we have a script on the bricks that handles collisions. If not, I’ll hopefully get there tonight and can update this), then it would look something like this:

void OnCollisionEnter2D(Collision2D co) {
     //whatever other logic here
    Log("We hit the brick!");   
}
//
//...other code in the class, maybe, placement doesn't matter.
//
public static void Log(string message, Object context = null) {
#if UNITY_EDITOR
    Debug.Log(message, context);
#else
    Application.ExternalCall("console.log", message);
#endif
}

Now, this isn’t great coding practice, because Log can now be accessed from anywhere in your code, but there’s no reason it should be on a brick, particularly. It would be better to put this on a GameManager or even a dedicated “Utilities” class. Because it’s public and static, though, you can call it from anywhere by using

ContainingClass.Log(yourString);

Compare this to if it were not static (but still public), in which case you need:

ContainingClass instance = new ContainingClass();
instance.Log(yourString);

That’s the crux of the “static” keyword – it means you don’t need to create an instance of a class in order to use the method.

And don’t worry, it’s not a silly question! As far as I’m aware, we haven’t covered calling methods from other methods in much detail. OnCollisionEnter will “pass control” to Log (which means log is now what the CPU is running). Log will execute (print its statement to either the Unity console or the Javascript console.log if we’re in browser), then it will pass control back to OnCollisionEnter, which at that point would just finish and your game would continue to run. The reason we can call Log from OnCollision enter is a little more complex. Let’s analyze:

void OnCollisionEnter2D(Collision2D co)
public static void Log(string message, Object context = null)

Since OnCollisionEnter2D doesn’t specify a protection level, it’s assumed to be private, which means nothing outside the class can reference it (you can’t call the object’s OnCollisionEnter2D from another type of object). However, since it’s within a class that can access Log (any class), we can still call Log from within it. Public doesn’t actually matter in this case, since it’s in the same class as OnCollisionEnter2D. But it would matter if we decided to put Log in GameManager or something similar, as mentioned above. Then, Log would need to be public so we could see it from another class (Brick, in this case).

Now, if that all seems a bit crazy, don’t worry – it is. We’re getting into the nitty-gritty details of Object-Oriented Programming with that. All you should need to remember is this:

Since it’s static, I can literally just call it from within another method (in any class that can access it), which will allow it to do what it’s supposed to, then we’ll return to the original method and continue as usual.

It’s important to emphasize that you can certainly call non-static methods (though not from within static methods), you just need to create an instance first.

Finally, don’t lose that confidence! This isn’t easy stuff, and it’s awesome that you got this far so quickly! It can be easy to lose determination when you encounter errors that aren’t your fault, but keep chugging! When you solve them, it’ll feel all the better!

Sorry this answer got super long, but I hope it helps! If you haven’t already, do try building for windows and seeing if that exhibits the same problems.

@TheAwesomeJames Any developments?

Kind of. I’ve grown frustrated building this game for an hour and a half each time only to have the same bug, so I’ve got a new computer on the way lol. All my files are backed up so shouldn’t have an issue transferring them. I was in dire need of an upgrade anyway as I was running an old core 2 duo and 4Gb RAM. 7th gen i7 and 16GB DDR5 should sort that out! Once it’s all sorted I’ll revisit this thread and give you an update!

1 Like

Hey @Gwynblade,

So I got everything set up on the new rig and dropped your Log code in. It didn’t return anything directly but poking through the browser debug I found this line;

28 blob:https://v6p9d9t4.ssl.hwcdn.net/383d503b-a931-4cde-995f-7e595f86160c:1 UnityException: GameObject has undefined tag!

(Filename: currently not available on il2cpp Line: -1)

I’m sure it’s not a coincidence that there’s 28 bricks in the first level of the game.

So in the course of the videos we’re told to tag the brick prefabs as either breakable or unbreakable. This has surely got to be it right? Now I’m thinking I’ve missed something in the videos.

I’ve double checked my code and I can’t see any errors at all. I’m getting so frustrated now. I really want to figure out how to fix this so I can learn from it in the future but I’m this >< close to starting again from scratch out of pure frustration now. IF anyone at all has any further advice they could offer I’d really appreciate it!

Hey James, stick with it we’ll get there.

Can you commit your project up to a GitHub repo so we can clone it and step through it? PSA: How to ask for help with Unity

Also have you compared your code to Ben’s original code on GitHub?

Hey @ninjachimp,

I’ve got the full project file uploaded to Google Drive right now (link in OP) I’ll have a fiddle with Github in a minute and try to figure out repos, only just signed up with them!

I have compared my code to the complete code included on the course materials and I can’t see any glaring mistakes, I’m hoping a fresh set of eyes will spot something I’ve missed. I’ve been stuck on this for a month now so I think confirmation bias is setting in in my head and I’ve convinced myself I haven’t made a mistake when I must have somewhere!

Updated the Google drive file, can now be found at this link

I had the same problem. For me what fixed it was go to the brick prefab assets, delete the breakable tag u put there, then re add a new breakable tag.

2 Likes

@Brent_Giles

Thank you so much mate! That was it!

Weeks and weeks of going over my code double and triple checking it all for the slightest mistake, and it was something as simple as that. Amazing.

I owe you big time.

1 Like

Yeah I was going through my code too, scratching my head haha. Try going to the Discord channel, one of the moderators there helped me out and the whole community seems pretty good on there if you need quick answers.

2 Likes

…the whole community is pretty good on here too :wink:

1 Like

Glad that got resolved!

1 Like