How can I fix a null pointer exception? (2d platformer tutorial)
Hi everyone!
I am pretty new to Unity, so I was following a tutorial for a 2D platformer game that I was interested in (https://www.youtube.com/watch?v=hn9lkAua3Vk&list=PLX2vGYjWbI0SUWwVPCERK88Qw8hpjEGd8∈dex=2).
However, I keep getting an error: NullReferenceException: Object reference not set to an instance of an object PhysicsObject.Movement (Vector2 move) (at Assets/scripts/PhysicsObject.cs:42) PhysicsObject.FixedUpdate () (at Assets/scripts/PhysicsObject.cs:36)
I seem to be matching the code as given by the instructor, but perhaps I missed something that I cannot seem to find. Does anyone know why I may be getting a null reference exception? Anything would be appreciated! I pasted my code below.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PhysicsObject : MonoBehaviour {
public float gravityModifier = 1f;
protected Rigidbody2D rb2d;
//other classes will inherit, but not accessible to outside classes
protected Vector2 velocity;
void onEnable(){
rb2d = GetComponent<Rigidbody2D> ();
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void FixedUpdate(){
//move our object downward each frame bc of gravity
velocity += gravityModifier * Physics2D.gravity * Time.deltaTime;
//determines the next position of the object based on gravity
Vector2 deltaPosition = velocity * Time.deltaTime;
Vector2 move = Vector2.up * deltaPosition.y;//we'll pass this to movement function
Movement (move);
}
//moves object based on values calculated, sets position of object's Rigidbody 2d
void Movement(Vector2 move){
rb2d.position = rb2d.position + move;
}
}
Answer by FortisVenaliter · Oct 10, 2017 at 07:04 PM
onEnable() won't be called unless it's spelled correctly as OnEnable().
Your answer
Follow this Question
Related Questions
How do you access Sprite renderer flip in a script? 0 Answers
Grenade damage script error 0 Answers
Destroying an object with a specific name 0 Answers
Car from Unity's standard assets giving me of null reference errors 0 Answers
NullReferenceException when using gameObject.SetActive(false); 0 Answers