NullReferenceException, can't call the Transform of objects

Hey all,
This may be a bit confusing but I hope I can get my point across and receive some help.
What I’m attempting to do is store the rotation of the camera in my scene and then call it when I choose. For this, I’ve been trying out bits of code and learning what I can, but due to my lack of deep understanding I’m running into a lot of problems. For now, I’m trying to do as described by calling the code for it when I press the “k” key:

public void CameraMovement()
{
    if (Input.GetKeyDown("k"))
    {
        Debug.Log("k");
        Quaternion cameraSnap = Quaternion.Slerp(transform.rotation, playerLocomotion.playerRotation, playerLocomotion.rotationSpeed * Time.deltaTime);
        transform.rotation = cameraSnap;
    }
}

What I’m attempting to do is call variables from another script, Player Locomotion. I want to use the values of these variables. I’m sure there’s plenty wrong with this code already, but I haven’t been able to troubleshoot anything because of a very glaring error I get when I try to run the game.

I’m familiar with the error “NullReferenceException” but this one is odd because I can’t seem to figure out what’s causing it.
The error only appears when I press k, so I’m assuming it’s my new code that’s the problem and not it interacting oddly with my original code. The new code is based on calling transforms. I clicked on the script itself to view it in the inspector, and found three missing transform references:
198441-delete-1.png
Nothing can be dragged into it regardless of if it’s a parent or child object, prefab, etc. I immediately thought I had to write in code to show the script to get to the transforms I referenced, but I’m confused on what I’m supposed to be calling. The names of the transforms in the inspector are completely different from the transform names I’ve called with this new code. Plus, when I originally checked the object this script was attached to, there were indeed some blank spaces for references. However, filling them did nothing for the code or the error.

If someone could tell me about any errors I’m making here and how I could fix them, I would greatly appreciate it as I’m nearly certain this issue is caused by multiple issues due to user error.

Hello!


First, must understand a script in a object scene is not the same as a script in your project folders. Every script (and actually overything) in your project folder is like a “idea” of the script. Once the script is dragged into a scene (or instantiated during play) it becomes an instance of that script, a “real one”. Then, in a script that is not in the scene, you can not drag elements from the scene, because that script is not there. When you say

“Nothing can be dragged into it
regardless of if it’s a parent or
child object, prefab, etc”

is because you are looking at the inspector of the script in the porject, not in the scene.


Second, about NullReference, you MUST LEARN TO DEBUGG your code while running. is 100% impossible to do any project without debbuging, constanlty, when you have errors like this, that need to find where exactly is, for example, a null reference. There are so many tutorials about debugging. This is the standard message i normally post for nullreference posts:

This is a standard answer.

Before coming Unity answers, you must always google what your error means. And learn that in every error message, it says the line of code where is happening.

Null Reference errors occurs when there is some variable with value null when code tries to read it. You need to learn to find your problem by your own. First, check your error code, it says the line where the problem is. Second, You need to debug the code while running, and check the states of the variables of the line at the moment the error occurs,

I’m sure you will detect what variable value is NULL. Then investigate why.

Look for some tutorials on how to debug code while running on your scripting software if don’t know what I’m talking about.


Third, You talk about

the names of the transforms in the
inspector are completely different
from the transform names I’ve called
with this new code

but we dont see any code assigning transforms, of any kind, if you talk bout something is not working or something you dont understand, we nee to see the code to help…


Bye!