Hello. I’m making a simple platformer game similar to the early spyro games. I’m using a character controller, and in my script I added a part that will be used to push objects by using addforce. Everything is working perfectly, but I’m getting this error message:
NullReferenceException: Object reference not set to an instance of an object
tryagain.OnControllerColliderHit (UnityEngine.ControllerColliderHit hit) (at Assets/tryagain.cs:44)
UnityEngine.CharacterController:Move(Vector3)
tryagain:Update() (at Assets/tryagain.cs:35)
Here is my code on the player. Ignore the massive list of trigger events, I don’t know how to organize it better :(.
using UnityEngine;
using System.Collections;
public class tryagain : MonoBehaviour {
public float moveSpeed = 30.0f;
public float rotateSpeed = 200.0f;
public float jumpSpeed = 15.0f;
public float gravity = 30f;
public float pushPower2 = 50.0F;
Vector3 currentMovement;
public CharacterController controller;
public GameObject spawn;
void Start ()
{
}
void Update ()
{
transform.Rotate (0, Input.GetAxis ("Horizontal") * rotateSpeed * Time.deltaTime, 0);
currentMovement = new Vector3 (0, currentMovement.y, Input.GetAxis ("Vertical") * moveSpeed);
currentMovement = transform.rotation * currentMovement;
if (!controller.isGrounded)
currentMovement -= new Vector3 (0, gravity * Time.deltaTime, 0);
else
currentMovement.y = 0;
if (controller.isGrounded && Input.GetButton ("Jump"))
currentMovement.y = jumpSpeed;
controller.Move (currentMovement * Time.deltaTime);
if (Input.GetKey(KeyCode.R))
transform.position = spawn.transform.position;
}
void OnControllerColliderHit(ControllerColliderHit hit)
{
Rigidbody body = hit.collider.attachedRigidbody;
body.AddForce (transform.forward * pushPower2);
}
void OnTriggerEnter(Collider other)
{
//if (other.gameObject.CompareTag ("betalevel2"))
//{
// Application.LoadLevel ("betalevel2");
//}
if (other.gameObject.CompareTag ("PickUp"))
{
other.gameObject.SetActive (false);
}
if (other.gameObject.CompareTag ("betalevel2"))
{
Application.LoadLevel ("betalevel2");
}
if (other.gameObject.CompareTag ("level 1"))
{
Application.LoadLevel ("level 1");
}
if (other.gameObject.CompareTag ("winlevel"))
{
Application.LoadLevel ("winlevel");
}
if (other.gameObject.CompareTag ("bouncy"))
{
jumpSpeed = 75.0f;
}
if (other.gameObject.CompareTag ("bouncy2"))
{
jumpSpeed = 200.0f;
}
if (other.gameObject.CompareTag ("level 1 prologue"))
{
Application.LoadLevel ("level 1 prologue");
}
if (other.gameObject.CompareTag ("cooljump"))
{
Application.LoadLevel ("cooljump");
}
}
}
The part that adds force is the OnControllerColliderHit function. I’m confused because other answers on this site say that the error message means the object doesn’t exist. The way I understand the code, the controller stores information of the object that was hit in the “hit” variable, and the rigidbody of the hit object is referenced and the force is added to it. I don’t know what’s wrong with it!
Like I said, it’s working exactly how I want it, but the console is SPAMMING me with error messages to the point where it’s lagging the play simulation in the editor. I’m quite new to unity and I’m using this as a big learning project so help on this problem is appreciated! Any other advice on my script is welcome too. Thank you!