- Home /
C#, "object reference not set to an instance of an object"
Using the CharacterController.Move script from the Unity Script Reference site, and I get an error that says "object reference not set to an instance of an object." I really have no idea what I'm doing wrong. Here's the code:
using UnityEngine;
using System.Collections;
public class Script_PlayerController : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
It would be helpful if we knew which line the error is on.
Oops sorry, the error is on this line:
CharacterController controller = GetComponent#CharacterController#();
replace the hashtags with < and >. Sorry about the multiple edits within the same $$anonymous$$ute, it kept deleting anything I posted within the < and >.
Cut and paste controller=GetComponent(); in void Start()
You don't need to Get the component in every Update and declare the CharacterController controller globaly
$$anonymous$$oving that line of code to anywhere but Update gives me errors. I agree, declaring it every update definitely sounds like a waste, but I get errors if goes anywhere else.
Also, not sure how to declare CharacterController controller globally. Adding a "public" before it gives me more errors, which I thought was the way to declare global variables 'n such?
You can also declare Character Controller Globally, like .... [RequiredComponent(typeof(CharacterController))] it shuld be declare before the class..
Answer by Euld · Dec 27, 2012 at 07:49 AM
Ok I think I've got it. The script is trying to access another script or component called Character Controller. It's possible to add this component through the menu by the way of Component -> Physics -> Character Controller. My Player was falling through the map for a while, but all I had to do was raise the Player up a bit.
Answer by anshul-bhardwaj · Dec 26, 2012 at 08:33 AM
use
CharacterController controller = GetComponent<CharacterController>() as CharacterController;
instead of :
CharacterController controller = GetComponent#CharacterController#();
The as is not necessary since the data type is already known because of the use of generics.
Nice try, but I'm still getting the same error as before. Removing the "as" gives me an error in $$anonymous$$onoDevelop.
Is there something I'm missing? Like should the object this script is attached to be named a certain way, or shouldn't have any children parented to it?
Answer by SirAlwayne · Feb 18, 2013 at 01:21 PM
CharacterController controller = gameObject.GetComponent();
that should fix ya
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Why am I getting this error ? 2 Answers
Get Boolean from another Script, C# 2 Answers
Help! How to change Real numbers to Natural numbers on Distance Counter script (Noob question) 0 Answers