The character controller return null refrence

I try to make my character move by using CharacterController but the problem is in the run time it tell me that the character controller does not match with any anything and the character do not move at all, so is there any falt in the code:

Here is my code:

using UnityEngine;
using System.Collections;

public class Move {
	


	Transform transform;
	float gravity  = 9.8f;
	float  speed  = 3f;
 	float jumpSpeed  = 0.3f;
	Animation animation;
	CharacterController controller = new CharacterController();
	Vector3 moveDirection = Vector3.zero;

	
	 public	void Moving() {
		
		controller = (CharacterController)controller.GetComponent(typeof(CharacterController));
	
	
		transform.Rotate(0, 2 * Input.GetAxis("Mouse X"),0);
	
		if(controller.isGrounded){
	
			moveDirection.x = 0;
			moveDirection.z = 0;
		
	
			if(Input.GetKey("up")){
			

				moveDirection = transform.TransformDirection(Vector3.forward);	
				controller.Move(moveDirection * Time.deltaTime * speed);
				animation.Play("walk");
			
			}
		
			if(Input.GetKey(KeyCode.Space)){
				moveDirection = transform.TransformDirection(Vector3.forward);	
				controller.Move(moveDirection * Time.deltaTime * speed);
				animation.Play("run");
			}
		
			if(Input.GetKeyUp(KeyCode.Space)){
		
				animation.Play("idle");
			}
		
			if(Input.GetKey("down")){
				moveDirection = -transform.TransformDirection(Vector3.forward)* 2;	
				controller.Move(moveDirection * Time.deltaTime * speed );
				animation.Play("walk");

			}
		
		}
	

		moveDirection.y -= gravity*Time.deltaTime;
		controller.Move(moveDirection * Time.deltaTime);

		}
	}

The line

controller = (CharacterController)controller.GetComponent(typeof(CharacterController));

Is getting the character controller component of the controller object, which doesn’t make sense. Try instead

controller = (CharacterController)transform.GetComponent(typeof(CharacterController));

I haven’t read your full code, but you can’t make an instance of a characterController with the new keyword like any other normal class. For all the unity components you should use the GameObject.AddComponent(“Type_Name”) method. Use it like you would use the new keyword:

CharacterController controller = gameObject.AddComponent(“CharacterController”);

And then you can call any of the components in controller as you normally would, like controller.Move() for example.

Notice, though, that the object’s inspector will only include the CharacterController in runtime and it won’t be saved when closing the editor. So you’d probably want to add that controller from the editor and then get it through code with GameObject.GetComponent(), you can use it like this:

CharacterController controller = gameObject.GetComponent(“CharacterController”);

This may come in handy especially when having to set the values for your component, like the dimensions for the collider, or any other value. You can then modify them in runtime and forget about modifying them through script. Much faster and simple.

That second method will not work if you have no character controller in that object before running the game and will result in a NullReferenceException. There is a way to force the object to have a required component through scripting, but I don’t remember it right now, a simple search on it may provide many good results though.

Hope it helped!

You can’t create new components (you can’t ever do “new CharacterController”, or any other type of Component). You have to use AddComponent.

Unless you’re changing things into characters on the fly, you’ll want to add the CharacterController component in the editor, not through script. Then your code will work (if you take out the new line, and have the controller on the same object as this script).