Object Reference Error

When I run it says “NullreferenceException: Object reference not set to an instance of an object”
image
Paddles


Balls

Hi Laurie,

That first screenshot would be the most useful one I suspect as it would show the full error but unfortunately it is too small to read, in fact, they all are.

With regards to your code, you can just copy/paste it into the forum and then add the code formatting characters in front and behind the code so that it is rendered nicely (see the link below).

Can you re-post the first screenshot at full size and then copy/paste your code please, there really isn’t anything to go on from your above post.


See also;

i apologise this is my first post.
image
Ball Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball : MonoBehaviour {
    public Paddle paddle;

    private bool hasStarted = false;
    private Vector3 paddleToBallVector;
    private Rigidbody2D rigi;
	// Use this for initialization
	void Start () {
        paddleToBallVector = this.transform.position - paddle.transform.position;
        rigi=GetComponent<Rigidbody2D>();
    }
	
	// Update is called once per frame
	void Update () {
        if (!hasStarted)
        {
            this.transform.position = paddle.transform.position + paddleToBallVector;

            if (Input.GetMouseButtonDown(0))
            {
                print("Mouse clicked, launch ball");
                hasStarted = true;
                this.rigi.velocity = new Vector2(2f, 10f);
            }
        }
    }
}

Paddle Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Paddle : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        Vector3 paddlePos = new Vector3(0.5f, this.transform.position.y, 0f);
        float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16;
        paddlePos.x = Mathf.Clamp(mousePosInBlocks,0.5f,15.5f);
        this.transform.position = paddlePos;
        
	}
}
1 Like

That’s ok, no problem, and thanks - I will take a look now for you :slight_smile:


Updated Sat Apr 07 2018 23:27

Ok, so the error message mentions that the problem is within Ball.cs and on line 13.

Looking at line 13 in Ball.cs we can see this;

paddleToBallVector = this.transform.position - paddle.transform.position;

Now, a nullreferenceexcption error is typically generated when you try to act upon an object for which you have no reference, and therefore it is null.

Looking at that line, you are setting a variable which you have declared in this script, paddleToBallVector, so we can rule that out. Next you refer to the position of the transform for the GameObject on which this script is attached, so it rules that out.

All that is left is;

paddle.transform.position

So, most likely, the paddle object.

If you look at your code at the very top you can see that this is a variable that you have set to be public so that you can drag your Paddle GameObject into this field within the Inspector. If you have a look, you’ll probably find that it isn’t set.

  • select the Ball within the Hierarchy of your scene
  • drag the Paddle GameObject from the Hierarchy into the exposed Paddle field within the Inspector
  • run the game.

Hope this helps.

By the way, the code formatting is three ` characters as opposed to three . characters :slight_smile:

Hope this helps and that you can move forward again, oh, and welcome to the community! :slight_smile:

Thank you, it works now.

1 Like

No problem, you are very welcome :slight_smile:

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

Privacy & Terms