I have some problems with my Script about fps D:

If an enemy shoots at me then I only get error messages and get no damage D:
Can anyone help me with that problem?

Problem in the script is at bullet script: Line 9/Line 31

MovementScript: (includes lifesystem of player on the bottom)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class PlayerMovementt : MonoBehaviour
{
    public float walkingSpeed = 7.5f;
    public float runningSpeed = 11.5f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    public Camera playerCamera;
    public float lookSpeed = 2.0f;
    public float lookXLimit = 45.0f;
    private float timer = 0.0f;
    public float points;
    public float maxHealth;
    public float health = 0;
    CharacterController characterController;
    Vector3 moveDirection = Vector3.zero;
    float rotationX = 0;
    [HideInInspector]
    public bool canMove = true;
    void Start()
    {
        characterController = GetComponent<CharacterController>();
  
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }
    void Update()
    {
        timer += Time.deltaTime;
        Vector3 forward = transform.TransformDirection(Vector3.forward);
        Vector3 right = transform.TransformDirection(Vector3.right);
 
        bool isRunning = Input.GetKey(KeyCode.LeftShift);
        float curSpeedX = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Vertical") : 0;
        float curSpeedY = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Horizontal") : 0;
        float movementDirectionY = moveDirection.y;
        moveDirection = (forward * curSpeedX) + (right * curSpeedY);
        if (Input.GetButton("Jump") && canMove && characterController.isGrounded && timer >= 1.0f)
        {
            moveDirection.y = jumpSpeed;
            timer = 0.0f;
        }
        else
        {
            moveDirection.y = movementDirectionY;
        }
 
        if (!characterController.isGrounded)
        {
            moveDirection.y -= gravity * Time.deltaTime;
        }
  
        characterController.Move(moveDirection * Time.deltaTime);
  
        if (canMove)
        {
            rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
            rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
            playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
            transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
        }
        if (health <= 0)
        {
            Die();
        }
    }
    public void Die()
    {
        print("Died");
    }
}

BulletScript:

using UnityEngine;
public class Bullet : MonoBehaviour
{
    public float speed;
    public float maxDistance;
    private GameObject triggeringEnemy;
    public float damage;
    private GameObject me;
    void Start()
    {
   
    }
    void Update()
    {
        transform.Translate(Vector3.forward * Time.deltaTime * speed);
        maxDistance += 1 * Time.deltaTime;
        if (maxDistance >= 5)
            Destroy(this.gameObject);
    }
    public void OnTriggerEnter(Collider other)
    {
        if(other.tag == "Enemy")
        {
            triggeringEnemy = other.gameObject;
            triggeringEnemy.GetComponent<Target>().health -= damage;
            Destroy(this.gameObject);
        }
        if(other.tag == "Player")
        {
            me.gameObject.GetComponent<PlayerMovementt>().health -= 20;
     
        }
    }
 
}

Thanks for Helping!!!

1 Like

This is just a wall of plain-text which nobody can read. Please edit your post and use code-tags when posting code.

NULL reference is the most common thing posted on the forums and is not a Unity thing. You should look up what it means as it’s easy to understand. No idea of your knowledge but it means you’re returning NULL on the line the compiler tells you (it does tell you the exact line and column and details if you read it) and then you try to do something with NULL like call a method.

Basically if you get it with “GetComponent()” it means that component wasn’t found so returns NULL but your code assumes you’re always going to find it so you do “NULL.DoSomething()” so you get a NULL reference exception.

Note that “PlayerMovementt” (two T’s) looks very suspect but regardless, it’s likely not finding that component.

1 Like

Here’s my three-step approach to fix nullrefs. You can do it too.

The answer is always the same… ALWAYS. It is the single most common error ever.

Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

This is the kind of mindset and thinking process you need to bring to this problem:

Step by step, break it down, find the problem.

Here is a clean analogy of the actual underlying problem of a null reference exception:

Thanks very much i will try this :smile:

Your ‘me’ variable, for which you may wanna consider finding a more appropriate name, is declared without a value, never assigned a value throughout your code, and private so it is also never assigned a value through the inspector. Meaning it is an empty reference pointing to nothing, or null in other words.

So when you later do me.gameObject.[…], you try to access some .gameObject attribute of null, which does not exist. If ‘me’ is supposed to be the player and not the same object, you likely intended to make the variable public and assign a value through the inspector.

And please… use code tags. This is unreadable. I simply used the search function for ‘me’ to gain some insight. Which is also why the name absolutely sucks, basically every second word got highlighted, so it was double extra effort to try and give some advice.

im very sorry for that crazy long text this was my first forum post :confused:

Thank you very much for your comment. “me” is the name of my player but in other scripts i didnt had problems at al but i will try it to set it on public and so on…

i changed it now :smile:

No problem and understandable. Just wanted to mention it because it’ll make other devs lives easier helping you. Good luck with your project.

Thanks very much!!