Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

You must login to add post.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Passionable Logo Passionable Logo
Sign InSign Up

Passionable

Passionable Navigation

  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • New Questions
  • Trending Questions
  • Must read Questions
  • Hot Questions
Home/ Questions/Q 9147
In Process
Alek Richter
  • 0
Alek RichterEnlightened
Asked: January 13, 20222022-01-13T14:07:10+00:00 2022-01-13T14:07:10+00:00

Unity Ошибка NullReferenceException: Object reference not set to an instance of an object [дубликат]

  • 0

Есть класс SaveData

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 };

}

А есть класс 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;
}

И на строчке

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

выдает ошибку NullReferenceException: Object reference not set to an instance of an object Motives.Start () (at Assets/Scripts/Motives.cs:40) Но при этом, если занести SaveData.current.health.value в переменную перед вызовом метода, и подставить эту переменную в вызов, то все работает корректно. Вопрос: как правильно использовать SaveData.current.health.value?

И конечно, сам Motive

public class Motive
{

public string name;
public float value;

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

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
    • Report
  • Share
    Share
    • Share on Facebook
    • Share on Twitter
    • Share on LinkedIn
    • Share on WhatsApp

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Alek Richter Enlightened
    2022-01-13T14:07:35+00:00Added an answer on January 13, 2022 at 2:07 pm

    Вы используете SaveData.current.health.value, но у вас current поле этого класса не инициализируется нигде, поэтому null.

    Объявите класс так:

    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 ;
    }

    }

    А вместо HudBar(-0.2f, 1.22f, SaveData.current.health.value, SaveData.current.health.name);

    напишите:

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 4k
  • Best Answers 0
  • Users 3
  • Popular
  • Answers
  • Alek Richter

    Truth value of a Series is ambiguous. Use a.empty, a.bool(), ...

    • 2 Answers
  • Alek Richter

    What is a NullPointerException, and how do I fix it?

    • 2 Answers
  • Alek Richter

    Make MS Word document look like it has been typeset ...

    • 2 Answers
  • Alek Richter
    Alek Richter added an answer Pandas DataFrame columns are Pandas Series when you pull them… January 13, 2022 at 2:21 pm
  • Alek Richter
    Alek Richter added an answer The handshake failure could have occurred due to various reasons:… January 13, 2022 at 2:19 pm
  • Alek Richter
    Alek Richter added an answer Mac OS X doesn't have apt-get. There is a package… January 13, 2022 at 2:18 pm

Top Members

Alek Richter

Alek Richter

  • 4k Questions
  • 1k Points
Enlightened
coinbaseprosupportnumber[Xsdwerfgtyujhnbgfderewqasdxz]

coinbaseprosupportnumber[Xsdwerfgtyujhnbgfderewqasdxz]

  • 2 Questions
  • 2 Points

Trending Tags

questin question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • New Questions
  • Trending Questions
  • Must read Questions
  • Hot Questions

© 2021 Passionable. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.