Game Goes to Lose Screen Randomly

I was testing out the sound clip on this lecture, and eventually got it to work, but I left my seat for a little less than a minute with the game running, paddle on ball, and found that it went to the “Game Over” screen.

Testing this a few times revealed that, though randomly, my game will always throw you into the Game Over screen.

It doesn’t do this while the ball is in motion. Only when it is stuck to the paddle. I never corrected the “ghosting” ball effect that Rick fixed by reordering two lines of code a few lectures back… could that be the culprit?

EDIT: Setting the script load order of the paddle and ball did not fix it.

When having this kind of issues what you gotta do is go to the source of what is going, in this case the script that throws you into the Game Over screen, How is that being trigger and why? If you don’t find anything in the scripts, then look in your hierarchy, maybe there’s somethings that is triggering it, an empty game object that you left there, or an extra ball that you forgot to remove.

Doing a bit of troubleshooting here… so far:

• If I remove the ball, I don’t get the issue.
• If I remove the lose collider, I don’t get the issue.

Here are my scripts for the ball and lose collider respectively:

Somehow, the lose collider (floor trigger) is being triggered by the ball, even when the ball is not moving. That it happens randomly is even more bizarre. They are nowhere near each other on the canvas, nor are there any duplicates.

This is going to be kinda hard to fix, I suggest you upload your entire project (Without the Library Folder) so people can test out the bug, unfortunately I don’t think this is a matter of just code, there are other things going on, also take into consideration that your OnTriggerEnter2D method can be triggered by anything.

In your method OnTriggerEnter2D method print the name of the collision.

print(collision.name); 

This will lead you to the issue.

Here’s what I’m getting… I got this error three times, about 8 seconds apart each.

ArgumentNullException: Value cannot be null.
Parameter name: source
UnityEngine.AudioSource.Play () (at /Users/builduser/buildslave/unity/build/Modules/Audio/Public/ScriptBindings/Audio.bindings.cs:820)
Ball.OnCollisionEnter2D (UnityEngine.Collision2D collision) (at Assets/Scripts/Ball.cs:53)

Hi Earl,

Does the AudioSource reference an audio clip?

1 Like

Hey Nina,

Sorry for the late reply. I was beating my head against the wall on this a bit, as another malfunction started to appear. The “clink” sound would play randomly while the paddle was still. I did not have it play on aware, and the AudioSource was referencing the clip. Maddening!

Ultimately, I sacked the build and switched to running Unity on my PC. Building and coding with only a trackpad was an exercise in masochism as it is. So, I’ve been slowly getting myself back to this point again, and that’s what accounted for the silence.

Fingers crossed I reach the threshold this time with no problems!

Alright. I’m getting the same error now. With even Rick’s NWUI files imported and not my own. And even a few lectures before!

So it’s just happened as soon as I glued the ball to the paddle. The LoseCollider is registering the ball passing through it, as identified by printing the collision name to the console.

I also get this error on building:

Assets\Scripts\Ball.cs(8,29): warning CS0649: Field ‘Ball.paddle1’ is never assigned to, and will always have its default value null

Edit: If I’m reading this right, Unity is telling me that I haven’t hooked up the Paddle to the Serialized Field we’ve coded as “Paddle 1” inside of the Ball Script component. Which I have.

My script is line for line the same as Rick’s on all accounts. Why is this happening?

Those messages with the yellow icons are harmless warnings, probably caused by a bug in Unity. Double click on each message in your Console, and add = default; behind the respective variable declaration.


What “clink” sound do you mean? Did you assign it to a specific game object? If not, check if there is “Play On Awake” enabled in one of the AudioSources in your game.


Unity’s physic engine is highly optimised meaning it checks the position of a collider occasionally only. You could try to set the Collision Detection mode of the ball’s Rigidbody2D to “Continuous”. In most cases, that fixes the problem.

In our particular case, the problem is that we manipulate the transform.position via code and override the calculated values of the physics engine. It is likely that the position of the ball does not match the position of the collider anymore.

A better solution would be to disable the physics simulation for the ball while manipulating the position via code.

void Start ()
{
    paddleToBallVector = transform.position - paddle1.transform.position;
    myAudioSource = GetComponent<AudioSource>();
    myRigidBody2D = GetComponent<Rigidbody2D>();
    myRigidBody2D.simulated = false; // <-------- add this
}
 
private void LaunchOnMouseClick()
{
    if (Input.GetMouseButtonDown(0))
    {
        hasStarted = true;
        myRigidBody2D.velocity = new Vector2(xPush, yPush);
        myRigidBody2D.simulated = true; // <-------- add this
    }
}

I hope that makes sense. :slight_smile:

Changing the collision detection from “Discrete” to “Continuous” seems to have fixed the issue. Thanks for the help!

I’m trying to understand your explanation of the issue though, just to grasp the concepts…

By overriding the physics simulator to keep the ball on the paddle, are we risking strange calculations by Unity in trying to “fight” that override randomly? Or perhaps on occasion, the “Update” method fails to keep the ball placement where it ought to be, and then the logic breaks, putting the ball in some random place in the gamespace for a millisecond?

Theoretically, to me that seemed to be the case. Especially in the first iteration of my code, where the ball would reference the Audio Clip at random intervals as well, as if it had hit a wall or a block.

I’m glad to have addressed the issue and found a fix… but 2 out of 2 times I followed and implemented this lesson’s exact code, which I would assume is an extremely small amount of code at that, it has produced atypical errors. Perhaps it’s something of an update in Unity since this video, that this type of code would now only be problematic?

Either way, I’ll try to stay hopeful as things only get more complicated from here on out. Thanks again.

Exactly. Unity’s physics simulation occasionally checks the actual position of the ball and compares it with the assumed position. Then the position of the collider and the ball game object get synchronised for a moment.

The position of the game object “Ball” is manipulated by both our code and the physics simulation while the collider is moved by the physics simulation only. If our code overrides the ball position of the physics simulation, it does not necessarily override the collider position along with it.

If you log the name of the other game object your ball collides with into your console, you’ll see that the ball collider indeed collides with game objects while the ball appears to stay at the same position.

Rick sets the Rigidbody2D component to “Continuous” at some point to fix the issue. It makes the physics simulation check the actual collider position based on the position of the game object more often.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms