[SOLVED] Object reference not set to an instance of an object

Hey everyone been a while since I posted but getting this error when trying to get autoplay to work.

error lines are saying
Paddle.AutoPlay () (at Assets/Scripts/Paddle.cs:26)
Paddle.Update () (at Assets/Scripts/Paddle.cs:20)

confused as all hell. Thanks for anyone who can have a looky.

P.S
I used the same ball in the assets folder given for this game so the name of the sprite is unchanged.

I can`t see the screenshot, please share it in full resolution or share the code using CodeShare or Paste Bin.

apologies

using UnityEngine;
using System.Collections;

public class Paddle : MonoBehaviour {

	public bool autoPlay = false;

	private Ball ball;

	void start (){
		ball = GameObject.FindObjectOfType<Ball> ();
	
	}
	
	// Update is called once per frame
	void Update () {
		if (!autoPlay) {
			MoveWithMouse ();
		} else {
			AutoPlay();
		}
	}

	void AutoPlay(){
		Vector3 paddlePos = new Vector3 (0.5f, this.transform.position.y, 0f);
		Vector3 ballPos = ball.transform.position;
		paddlePos.x = Mathf.Clamp (ballPos.x, 0.5f,15.5f);
		this.transform.position = paddlePos;
	}

	void MoveWithMouse(){
		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

I Think that error is within the start method, you’ve written it in lower case and it should be void Start(). The code within the start method never gets to run, so the ball reference never gets addressed to the intended script. This results into a null reference error when you try to set the value of the ball to the paddlePos.

1 Like

IT WORKS Joao you genius i been trying to wrap my brain around this all night was just a grammatical error with the lower case as you said thanks a bunch!

2 Likes

It is good to have someone else looking at our code, we usually are looking for the error in the wrong place and another pair of eyes tends to look at places that we have not :slight_smile:

1 Like

My biggest issue in anything literature is grammar, not exactly the greatest weakness to have for coding XD I look forward to going on to the next unit.

2 Likes