Sudden Trouble Loading start screen

Hello. I am working through the Friction section. I went to test the game flow and noticed that some errors that I can’t solve. Any help would be appreciated.

public class Ball : MonoBehaviour
{
// Config
[SerializeField] Paddle paddle1;
[SerializeField] float xPush = 2f;
[SerializeField] float yPush = 15f;

// State
Vector2 paddleToBallVector;
bool hasStarted = false;


// Start is called before the first frame update
void Start()
{
    paddleToBallVector = transform.position - paddle1.transform.position;

}

// Update is called once per frame
void Update()
{

    if (!hasStarted)
    {
        LockBallToPaddle();
        LaunchOnMouseClick();
    }

}


private void LaunchOnMouseClick()
{
    if (Input.GetMouseButtonDown(0))

    {
        hasStarted = true;
        GetComponent<Rigidbody2D>().velocity = new Vector2(xPush, yPush);

    }
}





private void LockBallToPaddle()

{
    Vector2 paddlePos = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
    transform.position = paddlePos + paddleToBallVector;
}

private void OnCollisionEnter2D(Collision2D collision)
{

    if (hasStarted)

    {
     GetComponent<AudioSource>().Play();
    }

}

}

Hi,

NullReferenceException means that a reference (“link”) to an instance is missing. If you exposed a field in the Inspector, make sure that it’s not empty.

Double click on the error message. Which line in your code gets highlighted?

Most certainly this reference to the paddle1 that is missing in the inspector. Select your Ball gameobject and look under Ball (Script)

Ball

There should be linked the paddle that we created earlier

Thank you so much for your help. I’m still getting the errors. My paddle was linked to the ball script. The game runs fine to a point. I can play level ok. Once I lose the game, the screen correctly switches to the Game Over screen. If I choose to play again, it goes to an earlier build of the game screen. The build settings show the correct order from 0-2.

Brian



Screen Shot 2019-06-05 at 11.53.47 AM.png

Holy cow. I fixed it. The problem was my logic. I was going from Loadstartscene when I should have been going to Loadnext scene.

Brian

Well done mate ! :wink:

Good job! :slight_smile:

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

Privacy & Terms