Object reference not set to an instance of an object

I always get this error message what should I do here’s the script

var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;

private var moveDirection : Vector3 = Vector3.zero;

var Fadenkreuz :Texture2D;

var shot :Transform;

function Update() {

if(Input.GetButtonDown("Fire1")){

var schuss = Instantiate (shot,Transform.Position,Quaternion.identity);

schuss.rigidbody.Addforce(Transform.Forward*300);

}

Screen.lockCursor = true;

    var controller : CharacterController = GetComponent(CharacterController);
    if (controller.isGrounded) {
        // We are grounded, so recalculate
        // move direction directly from axes
        moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
                                Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;
        
        if (Input.GetButton ("Jump")) {
            moveDirection.y = jumpSpeed;
        }
    }

    // Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;
    
    // Move the controller
    controller.Move(moveDirection * Time.deltaTime);
 }
 
function OnGUI() {

GUI.Label(Rect(Screen.width/2-25,Screen.height/2-25,50,50),Fadenkreuz);

}

Object reference is not set to an instance of an object is the bane of all programmers - what we know as a NullReferenceException. Long story short, something that you’re trying to work with does not have an instance defined!

var speed : float = 6.0; var jumpSpeed : float = 8.0; var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;

var Fadenkreuz :Texture2D;
var shot :Transform;

function Update() 
{
  if(Input.GetButtonDown("Fire1")){
    var schuss = Instantiate (shot,Transform.Position,Quaternion.identity);
    schuss.rigidbody.Addforce(Transform.Forward*300);
  }

  Screen.lockCursor = true;

  var controller : CharacterController = GetComponent(CharacterController);
  if (controller.isGrounded) {
    // We are grounded, so recalculate
    // move direction directly from axes
    moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
                            Input.GetAxis("Vertical"));
    moveDirection = transform.TransformDirection(moveDirection);
    moveDirection *= speed;

    if (Input.GetButton ("Jump")) {
      moveDirection.y = jumpSpeed;
    }
  }

  // Apply gravity
  moveDirection.y -= gravity * Time.deltaTime;

  // Move the controller
  controller.Move(moveDirection * Time.deltaTime);
}

function OnGUI() {
  GUI.Label(Rect(Screen.width/2-25,Screen.height/2-25,50,50),Fadenkreuz);
}

Problems that could be it:

  1. var schuss = Instantiate (shot,Transform.Position,Quaternion.identity);
    …consider using transform.Position, if you’re referring to the current GameObject’s transform.

  2. var controller : CharacterController = GetComponent(CharacterController);
    …Does the current game object have a CharacterController attached?

  3. GUI.Label(Rect(Screen.width/2-25,Screen.height/2-25,50,50),Fadenkreuz);
    …Is ‘Fadenkreuz’ assigned?

Those should narrow down the problem for you.