NullReferenceException: Object reference not set to an instance of an object

,

I have been trying to build my project but I get this error repeatedly

using Microsoft.Win32;
using System.ComponentModel;
using UnityEngine;
public class Moviment : MonoBehaviour
{
public Rigidbody Player;
public GameManager Gm;
public Pause pausa;
public float sideForce = 2000f;
public bool GameEnded;
void FixedUpdate(){
GameEnded = Gm.GetComponent (“GameHasEnded”); //the error is here
if (GameEnded == false && pausa.IsPaused == false)
{
if (Input.GetKey(“a”)){
Player.AddForce(-sideForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey(“d”))
{
Player.AddForce(sideForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if(Player.position.y < -0.1f){
FindObjectOfType().EndGame();
}
}
if(FindObjectOfType().IsPaused == true)
{
GetComponent().velocity = Vector3.zero;
GetComponent().angularVelocity = Vector3.zero;
}
}
}

And one of the errors is :
NullReferenceException: Object reference not set to an instance of an object
at Moviment.FixedUpdate () [0x00001] in Assets\Scripts\Movement.cs:13

In Unity works perfectly but when I build nope
Sorry my English isn’t really good :")
Thanks in advice

Can you please copy and paste your real code instead of retyping it? There are a bunch of things in here which wouldn’t even compile, and I wouldn’t want to comment on the code only for that to just be a copy and paste error.

1 Like

Thats the original code :"v

This line doesn’t make any sense to me. GetComponent returns a reference to the component, or null if the component doesn’t exist. But you’re assigning it to a bool. Are you sure this code even compiles?

GameEnded = Gm.GetComponent ("GameHasEnded"); //the error is here

The only way you could get that error on that line is if Gm is null. That means you probably forgot to drag your GameManager into the inspector for this script.

The other major issue here is you have a file called Movement.cs but your class is called Moviment. That is going to cause you issues. The names need to match.

Finally, like Joe-Censored said, GetComponent returns a component but you’re assigning it to a bool. That won’t compile. That’s why I asked if you’re sure this is the real code? Doesn’t unity give you some compiler errors right now?