Hello, I am pretty new to Unity, though I been coding for a good while now… I mostly have experience with JavaScript, HTML, some C++, and some Lua… JavaScript and C# are pretty similar and it more widely recommended to use C# with unity, so that’s what I started to do… While coding a small “test” game to kinda get use to unity I kept running into this error: ```
UnassignedReferenceException: The variable missile of PlayerMovement has not been assigned.
You probably need to assign the missile variable of the PlayerMovement script in the inspector.
UnityEngine.Transform.get_position ()
PlayerMovement.FixedUpdate () (at Assets/Scripts/PlayerMovement.cs:21)
I am not sure why I am having this since I did, in fact, put the variable for missile to be assigned in the inspector, and what I intended the code for works just fine... However, every time I try to move the player since it is using the variable it will flood the console with that error even though in game it works...
My PlayerMovement Script:
```csharp
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public Rigidbody rb;
public Transform missile;
void FixedUpdate () {
if(Input.GetKey("w") && missile.position.y < 58)
{
rb.constraints = RigidbodyConstraints.None;
rb.AddForce(0, 40 * Time.deltaTime, 0);
}
else if (Input.GetKey("s") && missile.position.y > 40)
{
rb.constraints = RigidbodyConstraints.None;
rb.AddForce(0, -40 * Time.deltaTime, 0);
}
else
{
rb = GetComponent<Rigidbody>();
rb.constraints = RigidbodyConstraints.FreezePositionY;
}
}
}
omg, I feel stupid… So basically I had two copies of the script in the inspecter… I had one that I assigned the variables and the other which was at the bottom a bit hidden from me was unassigned so it caused the error… Thanks for your help though…
so its saying
UnassignedReferenceException: The variable theRB of PlayerController has not been assigned.
You probably need to assign the theRB variable of the PlayerController script in the inspector.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Rigidbody2D theRB;
public float moveSpeed = 5f;
private Vector2 moveInput;
private Vector2 mouseInput;
public float mouseSensitivity = 1f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
moveInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
theRB.velocity = moveInput * moveSpeed;
}
}
The error message tells you exactly what you need to do. Look at the object in the inspector, find the “theRB” field, and drag a rigidbody into that slot.
Please don’t reply to a two-year old thread for the simple most common error in the entire world.
The answer is always the same… ALWAYS. It is the single most common error ever. Don’t waste your life on this problem. Instead, learn how to fix it fast… it’s EASY!!
Some notes on how to fix a NullReferenceException error in Unity3D
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:
help i don’t see anything wrong bc i’m new actually and i’m folling a tutorial (course to make 3 games in unity) and it gave me that error and this is script;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class BallHandler : MonoBehaviour
{
help this is a different error and i have over 999+ of it i think my game is going to crash it says: NullReferenceException: Object reference not set to an instance of an object, help help i’m going to die of sadness and rage here
If you are seeing an enormous pile of errors, then it’s probably happening every frame (or multiple times per frame).
A little tip: when this happens, go find the first error. An exception being thrown during startup can leave components only partially initialized, leading to tons of other errors – errors that will go away once you deal with the original problem.
For example, I recently had something like this…
“NullReferenceException on line 107”
“NullReferenceException on line 123”
“NullReferenceException on line 123”
“NullReferenceException on line 123”
“NullReferenceException on line 123”
[…]
Line 107 threw an exception because I forgot to assign something. Line 108 was then supposed to set something else up that line 123 depended on.
There was nothing wrong with line 123, even though it was spamming triple-digit numbers of exceptions at me. I could have wrapped it in a null-check, but the field it used should never be null, ever, so that wouldn’t be the right fix.