[SOLVED] I have a very well hidden bug causing game over

I fixed the ball is breakable bug but I have another bug that I cannot locate the trigger for. It seems to happen at random times so maybe it’s a script execution order issue or a calculations speed issue I simply can’t find it because it hard to reproduce with any level of certainty.

Occasionally, either right after clicking start OR right after a new level loads once the prior level is cleared, the game goes straight to the lose screen as if the ball collided with the lose collider.

I can’t locate anyplace in my scripts that would call the lose level except for the lose collider script so it’s a mystery that a collision between ball and lose collider is being triggered somehow.

the only things I can think of doing so far is searching all of my scripts for the word Lose and it only exists in the Losecollder.cs and only in 2 places. The function LoseCollider and the load level “Lose”.

Please note, I have permission to freely use everything in this game. I’m friends with Stang. :slight_smile:

Here is a link to my entire project zipped on dropbox:

My Block Breaker Game

EDIT: I should mention I’m using Unity 5.1.2f1

EDIT 2: I don’t know if this has anything to do with it but I did notice on some levels, in the hierarchy, where you can see the audio devices getting created and then going away, I get some that don’t (in the list), sometimes it just gets longer and longer with just the few at the bottom appearing then disappearing.

Hi Lenny,

What type of collision detection do you have set on the Rigidbody for both the ball and the paddle?

Here are my settings.

Screenshots

Also, it happened on Level9 with game set to AutoPlay. I was very hopeful it would not happen at all with autoplay since that’s the longest run I’ve seen without it occurring. But then, it happened. Level 9 cleared - went to LOSE screen.

EDIT - Fixed screenshot link.

Hi Lenny,

Try the same test with the ball’s Rigidbody collision detection set to Continuous.

It’s quite hard for me to help with this one as I’m out until Friday and can’t open your files on my phone with Dropbox, but as Robs helping you, you should have it fixed soon. If it’s not solved by the time I’m home I’ll take a look but if I’m honest if Rob can’t do it then I doubt I can. Hopefully it’s fixed soon. :slight_smile:

1 Like

Very kind words @Recable, thank you.

@Lenny_Rivera, I have spent a few hours playtesting your game this evening, primarily with auto-play enabled, and I haven’t been able to reproduce the issue that you have mentioned.

My belief is that your descrete collision detection on the ball is the issue. I believe that on occasion, the ball is able to pass through the paddle and hit the lose collider, triggering the loading of the Lose scene.

I have added a Debug.Log statement within the OnTriggerEnter2D method to output what has collided but I have yet to have the issue and thus not seen the output. The code I added is as follows;

using UnityEngine;
using System.Collections;

public class LoseCollider : MonoBehaviour 
{
    private LevelManager levelManager;
	
    void OnTriggerEnter2D (Collider2D trigger) 
    {
        Debug.Log(trigger.name + " collided and made us lose");
		
        levelManager = GameObject.FindObjectOfType<LevelManager>();
        levelManager.LoadLevel("Lose");
    }
}

The plan being, when this event is triggered, we find out what game object made it happen, I fully expect it to be the ball.

If you apply this to your game and then run your tests again, let me know what occurs.

Incidentally, level 9 has some issues with bricks being over the top of other bricks, not sure whether you know or not, but it causes an odd flickering during the game as only half of a brick gets displayed. I thought I’d mention it now in case you spot it later and wonder what is happening. You also have a case where there is a duplicated brick on top of itself. Best way to resolve is to expand the different groups in the hierarchy, click on the top one in the hierarchy, then arrow down and see what is selected in the scene - you’ll soon spot them when the selection doesn’t move (or partially moves).

Hope this is of help :slight_smile:

If you find that it is the ball causing the collision with the lose collider, change the collision detection mode to Continuous (as above) for the ball, and potentially the paddle (as you have several levels with bricks near the bottom of the scene) and that should remedy the issue.

Hi Rob, thank you so much for your help. I am going to use this debug code first and report back.

Bricks directly on top of iother bricks is not something I want, bricks half on top of other bricks was me being lazy about creating half bricks. I will do that as well.

1 Like

You are more than welcome, let us know how things go. :slight_smile:

I’m having great difficulty replicating the issue this morning. I also am going through all of my levels and getting rid of brick on brick action finally. Will also create half bricks for there they are needed (mostly level 13).

EDIT:Perhaps I just needed to restart unity after a reboot? I am on Windows and we all know stranger things have happened.

An unrelated issue, I though I fixed my boring loop issue with some suggestions on the topic but no, it creeps up as well. No worries, I’m going to put triangle spikes on the left and right walls to prevent that from ever happening again.

I’m very happy to see so much active support here. I will help when and where I can whenever I can. I’m still just a noob and learning C# for the first time as i go although I have exposure to other, similar languages, it’s still a challenge. But I digress. Have more work to do on this game (just now getting to particles) so I will be play testing like crazy and will certainly report back if this issue occurs again. Thanks again for that debug code. Leaving it in until I’m done and saving it out into my code refrence doc I’m creating for myself. :slight_smile:

1 Like

Hey Lenny,

That’s odd isn’t it, but I suppose if it isn’t reliably reproduce able then, in one sense, that’s a good thing :slight_smile:

Regarding the boring loops, I did notice it, arguably quite a lot on scene 5, but the issue does exist across the board. Your game actually produced an instance of this issue which I hadn’t seen before, which was a diagonal boring loop! Top left to bottom right, that was at least a different version I hadn’t seen. I didn’t mention it before as I felt yoy had enough on your plate and, after yoy have been so actively creating more levels for your game I didn’t want to start listing issues.

There are a number of approaches to removing/reducing this issue, give your triangles a go and see what happens - fingers crossed :slight_smile:

I will help when and where I can whenever I can. I’m still just a noob and learning C# for the first time as i go although I have exposure to other, similar languages, it’s still a challenge.

Fantastic! It is that positive and supportive attitude which helps keep this community such a great place. So, thank you in advance.

Also, never under estimate how helpful a question can be, often there will be multiple people experiencing the same problem but maybe they lack the confidence to ask.

A posted question gives people an opportunity to see a solution, perhaps several, along with the opportunity to say, “I was having the same issue.” and then take part in the discussion.

I will look forward to seeing updates to your game(s) as you progress through the course. :slight_smile:

1 Like

I finished cleaning up all my levels I think (from brick on brick action), I just have to create some half bricks for the final level so I can clear that up too. It’s more aesthetic since I have to shift blocks over a half grid to spell words leaving the background unfinished. Once I finish that part, I’m going to make the triangle blocks. There will be only two per level, one on each left and right wall. That should take care of the left side right side boring loop once and for all. I have never seen a diagonal boring loop happen. Once thing I have to say, the theme for my game “subgenius” leaves room for quirky behaviour (know your target audience) so if it ever happened out in the wild, it’d be excusable LOL.

Once I complete those to side quests, I’m going to start the particles chapter. I love particles but hate ost of the particle systems I’ve ever dealt with. Hopefully Unity’s PS isn’t that bad. Just an FYI, for video post production (or animated sprites) I love Wondertouch’s Particle Illusion.

Thanks for the comments, nothing makes me happier than a active and respectful community; when it coms to support.

Still no sign of that bug I initially reported. Perhaps it was being triggered by a combination of other issues (extra borig loop code not implemented and duped bricks).

EDIT: PS - You should try my text adventure. :slight_smile:

1 Like

Well done on the clean up and the progress. Particles will add a little more life/action to your game, and they are fairly straight forward to implement.

One other thought, as you have a “head” for your ball, it could be quite fun if you could get it to rotate, that rotation may need to change direction based on an impact to look “correct” as it were, just a thought - as you say “quirky behaviour” etc :slight_smile:

I will check out your text adventure today :slight_smile:

I have wanted that (ball rotation/friction on impact) but haven’t investigated it yet. was hopeful it’d pop up in the lessons.

So I was adjusting some crazy levels (too hard, not winnable by humans, etc…) when I walked away for a bit as the AutoPlay had it’s way with level 12 (not the one you have, I changed it) and when I came back, the game was over.

It was indeed the ball that cause it.

I also found another errors in the Console -

UnassignedReferenceException: The variable crack of Brick has not been assigned.
You probably need to assign the crack variable of the Brick script in the inspector.
UnityEngine.AudioSource.PlayClipAtPoint (UnityEngine.AudioClip clip, Vector3 position, Single volume) (at C:/buildslave/unity/build/artifacts/generated/common/modules/AudioBindings.gen.cs:685)
Brick.OnCollisionExit2D (UnityEngine.Collision2D col) (at Assets/Scripts/Brick.cs:31)

Which is odd because the crack sound works fine.

I’m going to immediately change the Rigidbody collision detection to continuous and let it auto play again. I’m afk a lot today, have work to do so I will be picking this up tomorrow morning and follow-up at that time.

Going to ignore the audio error for the moment. Will dig deeper tomorrow.

EDIT: Ok I tried to ignore the audio error but then I noticed it harkens back to a previous observation I made regarding the hierarchy list. The crack sound pops up and disappears in the hierarchy list as “One Shot Audio” but when a lot of collisions take place, some do not get deleted. I’ve included a partial screenshot below showing one such instance stuck in the hierarchy window and the error in the console with the ball near the paddle so you know there is no brick collision occurring.

Partial ScreenShot One Shot Audio issue

On a side note, you mentioned never seeing a diagonal boring loop occur except in my game. Well, have you seen a horizontal boring loop where the ball is continuously bouncing off the paddle? Because that is what’s happening in that screenshot. The ball is only a few pixels above the paddle moving left and right quickly with autoplay keeping it alive. There is nothing to fix here since a player (and I did it myself after unpausing) can adjust the paddle position and hit the ball off the edge sending it upwards; hence, escaping the boring loop. :slight_smile:

1 Like

Hi Lenny,

Ok, I think there are three many points you are raising here;

  • Lose Screen / Collider / Ball - I am fairly confident that changing your collision detection mode will resolve this, but lets see how you playtesting goes.

  • Audio Error - Ok, I think before you can consider the playtesting to check for the improvement with the collision detection this error/issue needs to be resolved first.

    Often it’s a case of the object being destroyed before a piece of code gets to be called, although in this scenario, if you are trying to play the crack sound effect then I assume that you are not actually destroying the brick object at this point.

    See if you can debug this one, perhaps using Debug.Log to pop a few messages out to the console to try and determine what is happening at what stage. Often a debug message not appearing is just as useful when you are expecting one, as you can trace the issue backward from there. If you run into any difficulties, let me know and pop a link up to the current version of your project and I will happily to take a look. :slight_smile:

    I would imagine that the reason they are left behind in the Hierarchy, again, just a guess at this point, is because the code generates an error before the clip is played / destroyed. So, an object gets instantiated, but then errors and gets left behind.

  • Boring Loops - Yep, I have seen the horizontal one, that’s a whole load of fun… typically it’s either vertical or horizontal, which made your diagonal one the other day actually quite refreshing :slight_smile:

    I have also seen the horizontal issue where the player would be unable to resolve it with the paddle, but in that scenario gravity was not turned on, and the tweak wasn’t sufficient.

Note: That tinypic website causes me some headache with the screenshots. They seem to appear the first time, but if I go back to the site they don’t appear, just all the adverts. You can upload your screenshots directly into your posts, you can even copy/paste them in. If you are experiencing any difficulties with this do let me know, it’s a lot easier to see the screenshots when they are embedded in your post :slight_smile:

Ya know what would be amazing? To be able to step backwards in a program execution. Like, hit a check box to put Unity is a special playback mode that will allow you to step through say 100 steps and it records (memory dumps whatever) the game state for each step upto x (x<=100, x>=1) so you would be able to step backwards through code with a step counter in the debug console.

This is why, Paying atention to the console output while slowly stepping through the game I witnessed multiple One Shot Audio instances being created, my eyeballs jumped to the game screen to see what happened and it is likely to be that the ball hit 2 bricks at once. But I would love to be able to step back one (or a few) steps to be certain.

Still debugging. BBIAB.

EDIT: OK, I’m stumped. I setup two print statements around the audioclip created and brick destroyed behaviours. I couldn’t catch anything notable. I then stepped through the code carefully observing every frame and what was happening. Nothing weird at all. Everything was running just fine and them whamo, out of nowhere, the error. And with no extranious One Shot Audio instances in the hierarchy.

Here’s my project:
Project files

and here’s the instant it happend (screenshot):

Will try to take a look this evening. Any indication of events up to this point, so I can try to replicate without having to play test all levels?.

  • which scene did it occur on?
  • was it the last brick on the scene to be destroyed?
  • was the ball bouncing off of lots of bricks rapidly?

That kind of thing. :slight_smile:


Updated Sun Oct 01 2017 08:48

Hi Lenny,

Ok, so I’ve just ran the game and almost immediately received an error regarding the crack sound effect and the Inspector also showed two audio clips.

I am fairly confident the reason they are not being destroyed is that the code is erroring and halts the execution of code before it gets the chance.

Here’s a screenshot of what had happened at the time;

image

As you can see, Bob has collided with a diamond block. Whilst these are indestructible, they are still detecting a collision, and the collision code in Brick.cs says “Play the crack sound”, which isn’t referenced in the Inspector for the indestructible bricks…

	void OnCollisionExit2D (Collision2D col) {
		AudioSource.PlayClipAtPoint (crack, transform.position, 0.6f);
		print ("Crack sound created");
		
		if (isBreakable) {
			HandleHits ();
		}
	}

So, in the above, it would seem reasonable to move the top two lines of code to inside the if statement.

	void OnCollisionExit2D (Collision2D col) {
		
		if (isBreakable)
        {
            // only play the cracked sound if the brick is breakable
            AudioSource.PlayClipAtPoint(crack, transform.position, 0.6f);
            print("Crack sound created");

            HandleHits ();
		}
	}

That change resulted in the first scene being played on autoplay without error and the console shows corresponding output messages for each brick;

image

Note, if you want to do something a little more fancy, you could change the code so that if the collision is with an indestructible brick, a different sound clip is played, by using the other tag. Alternatively, you could add a different AudioClip to the indestructible brick prefab/instance, if you do this, it might be worth changing the name of the exposed AudioClip field on Brick,

public class Brick : MonoBehaviour {
	public AudioClip crack;

to something more like;

public class Brick : MonoBehaviour {
	public AudioClip collisionSoundEffect;

That way, the name of the field would be relevant for all bricks when viewed in the Inspector.

I won’t look any further until I hear back from you, as getting some reproduction steps would be handy (as above), or, maybe this was all the issue was.

Hope this helps :slight_smile:


p.s. Just looked at your screenshot again, whilst Bob hasn’t collided with an indestructible brick in that case, I suspect that another brick has. To test this theory, pop a Debug.Log statement in the collision code Brick.cs and output what collided. I think you are going to find that it was a brick colliding with the indestructible brick which then tried to play it’s cracked sound effect, and couldn’t because it doesn’t have one associated. The above code change should resolve this also, but it would be good to see if this is the issue before fixing it, just so you can see exactly what is going on :slight_smile:

Example;

	void OnCollisionExit2D (Collision2D col) {

        Debug.Log("Collision with object named : " + col.gameObject.name");

        AudioSource.PlayClipAtPoint(crack, transform.position, 0.6f);
        print("Crack sound created");

        if (isBreakable)
        {
            HandleHits ();
		}
	}

My apologies, had to do a family outing then after that left town for about a week on business. Back now. Will make the code adjustments yu suggested and see where it takes me now.

EDIT: Ok that fixed it. Now I have another issue I will open a new topic for.

1 Like

Privacy & Terms