Home c# Unity Error NullReferenceException: Object reference not set to an instance of an...

Unity Error NullReferenceException: Object reference not set to an instance of an object [duplicate]

Author

Date

Category

There is a SaveData class

public class SaveData
{
public static SaveData current;
public Motive health;
public Motive eat;
public Motive sleep;
public SaveData ()
{
  health = new Motive () {name = "health", value = 100};
  eat = new Motive () {name = "eat", value = 100};
  sleep = new Motive () {name = "sleep", value = 100};
}

And there is a class Motives

public class Motives: MonoBehaviour {
public static Motives Instance {get; set; }
public bool hudOn;
public GameObject hudback;
public TextMesh HealthText;
public TextMesh EatText;
public TextMesh SleepText;
public GameObject HealthImg;
void Start ()
{
  GameObject gma = GameObject.Find ("DialogBox");
  gma.SetActive (true);
  hudOn = true;
  HudBar (-0.2f, 1.22f, SaveData.current.health.value, SaveData.current.health.name);
  GameObject gm = GameObject.Find ("DialogBox");
  gm.SetActive (false);
  hudOn = false;
}
public void HudBar (float xpos, float ypos, float scale, string names)
{
  GameObject gm = Instantiate (HealthImg, new Vector3 (0.125f, 1.29f, 0), Quaternion.identity) as GameObject;
  gm.transform.SetParent (GameObject.FindWithTag ("back_hud"). transform);
  gm.transform.localPosition = new Vector3 (xpos - xpos * (100 - scale) / 100, ypos, 0);
  gm.name = names;
}

And on the line

HudBar (-0.2f, 1.22f, SaveData.current.health.value, SaveData.current.health.name );

throws NullReferenceException: Object reference not set to an instance of an object
Motives.Start () (at Assets / Scripts / Motives.cs: 40)
But at the same time, if you put SaveData.current.health.value into a variable before calling the method, and substitute this variable into the call, then everything works correctly. Question: how to use SaveData.current.health.value correctly?

And of course, Motive itself

public class Motive
{
public string name;
public float value;
public Motive ()
{
  this.name = "";
  this.value = 0;
}
}

Answer 1

You are using SaveData.current.health.value , but your current field of this class is not initialized anywhere, therefore null .

Declare the class like this:

public class SaveData {
  private static SaveData current;
  public Motive health;
  public Motive eat;
  public Motive sleep;
  public SaveData () {
    health = new Motive () {name = "health", value = 100};
    eat = new Motive () {name = "eat", value = 100};
    sleep = new Motive () {name = "sleep", value = 100};
  }
  public static SaveData GetInstance () {
    if (current == nul) {
      current = new SaveData ();
    }
    return current;
  }
}

And instead of HudBar (-0.2f, 1.22f, SaveData.current.health.value, SaveData.current.health.name);

write:

HudBar (-0.2f, 1.22f, SaveData.GetInstance (). health.value, SaveData.GetInstance.health .name);

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions