Basic 2d platformer movement script creates errors.

Hello!

I would need some help witht he following:

NullReferenceException: Object reference not set to an instance of an object
PlayerController.Update () (at Assets/Player/Scripts/PlayerController.cs:28)

My unity keep telling me this and i guess. that Unity wants a value on something but i dont understand what and how to do that. I am a beginner at scripting and havent fugured out all the know hows yet so please awnser like i was an idiot :slight_smile:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(PlayerPhysics))]
public class PlayerController : MonoBehaviour {
	
	// Handling
	public float speed = 8;
	public float acceleration = 12;
	
	private float currentSpeed;
	private float targetSpeed;
	private Vector2 amountToMove;
	
	private PlayerPhysics PlayerPhysics;
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		targetSpeed = Input.GetAxisRaw("Horizontal") * speed;
		currentSpeed = IncrementTowards(currentSpeed, targetSpeed,acceleration);
		
		amountToMove = new Vector2(currentSpeed,0);
		PlayerPhysics.Move(amountToMove * Time.deltaTime);
	}
	
	
		
	// Increase n towards target by speed
	private float IncrementTowards(float n, float target, float a) {
		if(n == target) {
			return n;	
		}
		else {
			float dir = Mathf.Sign(target - n); // must n be incresed or decreased to get closer to target
				n += a * Time.deltaTime * dir;
			return(dir == Mathf.Sign(target-n))? n: target; //If n has now passed target then return target, otherwise return n
		}
	}
}

Would apriciate help very much, thank you!

try using a lower case ‘p’ for your playerPhysics var… i think it’s getting confused between the component type PlayerPhysics and you variable.

note capital R in the requiredComponent and lower case r in the reference.

That was actually making sense but never the less it didnt give unity what it wanted and still states Null… hmmm…

Wll i figured it out after some time of stairing blank into the code.

private PlayerPhysics playerPhysics;
	
	// Use this for initialization
	void Start () {
		playerPhysics = GetComponent<PlayerPhysics>();
	}
	
	// Update is called once per frame
	void Update () {
[CODE]

I just figured, shouldent it not suppose to be something there :) 
LoL´s at me! Thank you though LeftyRighty for your time!