Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Want to make an FPS Shooter but need help with the code

Discussion in 'Scripting' started by oligamestv35, Nov 26, 2020.

  1. oligamestv35

    oligamestv35

    Joined:
    Nov 25, 2020
    Posts:
    2
    Heres My Code:


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class MouseLook : MonoBehaviour
    {
    public float mouseSensitivity = 100f;

    public Transform playerBody;

    float xRotation = 0f;

    void Start()
    {
    Cursor.lockState = CursorLockMode.Locked;
    }


    void Update()
    {
    float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

    playerBody.Rotate(Vector3.up * mouseX);

    xRotation -= mouseY;
    xRotation = Mathf.Clamp(xRotation, -90f, 90f);

    transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);

    }

    }

    ------------------------------

    So the problem is that an Error comes up saying:
    UnassignedReferenceException: The variable playerBody of MouseLook has not been assigned.
    You probably need to assign the playerBody variable of the MouseLook script in the inspector.
    ----------------------------
    If someone could help it would be really nice

    ty in advance
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    I'll basically repeat what the errors tell you here, but:
    You have a variable called playerBody. It contains nothing. Meaning it's "unassigned" or "null". This is one of the most common errors you will get. You need to fill it with something. As the next part suggests, you probably meant to drag an object in there through the inspector. Click on the gameobject containing your MouseLook script. In the inspector there will ne an empty slot named Player Body. You can drag the desired gameobject in there.
     
    Last edited: Nov 26, 2020
  3. oligamestv35

    oligamestv35

    Joined:
    Nov 25, 2020
    Posts:
    2
    Well since im making a first person game i assigned the main camera to it but it just breaks the whole script after i assign it i can only look up and down.

    My script works just fine but the error is spamming the whole console and i dont know how to fix it because when i try to fix it it breaks everything as i wrote above.
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Well, you are editing two different objects there. You are editing the X-Axis rotation of the object the script is attached to (transform.localRotation =... ), and you are editing the Y-Axis rotation of the assigned object (playerBody.Rotate(..)). This is probably not what you intend to do. There are hundreds of camera tutorials out there, i suggest following one.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception

    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:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

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