Find Text Component error

Im trying to set a text from another class but i get this error:

NullReferenceException: Object reference not set to an instance of an object
QuestManager.ShowQuestInfo (.Quest quest) (at Assets/QuestManager.cs:93)
QuestManager.Awake () (at Assets/QuestManager.cs:17)

This is my Quest class:

public class Quest {

    public int id;
    public string questName;
}

and this is my set text code and this is also where it gives me the error:

        UIManager.instance.questInfoContent.Find("Name").GetComponent<Text>().text = quest.questName;

and this is the questinfocontent holder:

        questInfo = canvas.Find("QuestInfo");
        questInfoContent = questInfo.Find("Background/Info/Viewport/Content");

Would be so glad if you could help :smile:

Show us the entire QuestManager script if you can. What you show has little meaning for how it connects to the error. As it stands, You’re making a call from Awake to ShowQuestInfo and then on line 93 you are running into null.

So the only thing I can tell you is either your GetComponent call isn’t finding anything, thus returning null or your quest object is null.

    public void ShowQuestInfo(Quest quest)
    {
       
        UIManager.instance.questInfoContent.Find("Name").GetComponent<Text>().text = quest.questName;
        UIManager.instance.questInfoContent.Find("Description").GetComponent<Text>().text = quest.description;

That is the function and it gives me error on the “uiManager” lines

So, you’ll need to figure out what is null here.
Before these two lines do some debug statements.

Debug.Log(UIManager.instance);
Debug.Log(UIManager.instance.questInfoContent);
Debug.Log(UIManager.instance.questInfoContent.Find("Name").GetComponent<Text>());
Debug.Log(quest.questName);
Debug.Log(quest.description);

And see which line it errors out at. Then you have to figure out why it’s null.

It doesnt show questName and Description, i guess that means my QuestClass doesnt work, and if so do you know how to fix?

Break apart your big statement:

UIManager.instance.questInfoContent.Find("Name").GetComponent<Text>().text = quest.questName;

something like so:

 GameObject go = UIManager.instance.questInfoContent.Find("Name");
Debug.Log( go);
Text t = go.GetComponent<Text>();
Debug.Log(t);
Debug.Log(quest);
t.text = quest.questName;

That will reveal which statement is failing. (I typed the above without trying to compile it… might have a typo but you get the point… pull your compound blob of code into its constituent pieces, which is almost always a better way to go anyway since the compiler will optimize!)

1 Like

gives me null on this one

Debug.Log(quest);

So your quest variable is null. Make sure you are assigning something to it.

its assigned to this other class i think

public class Quest {

    public int id;
    public string questName;
}

That is the class definition itself. You need to make an instance of it, generally speaking with a construct like so:

Quest myQuest = new Quest();
myQuest.questName = "What is your favorite color?";
myQuest.id = 1234;

Once you have that, you can pass myQuest into anyplace that needs a Quest object.

ObMontyPython: “African or European?”

I have a json file for that, can u tell me if this is done right?

Quest newQuest = JsonUtility.FromJson<Quest>(Resources.Load<TextAsset>("JsonFiles/" + id.ToString("00")).text);

and this is the json:

{
    "id":0,
    "questName":"Zombies Everywhere!"
}

You’ve got another nightmare of multiple things on one line. So once again, break it apart:

Quest newQuest = JsonUtility.FromJson<Quest>(Resources.Load<TextAsset>("JsonFiles/" + id.ToString("00")).text);

You’re doing at least four things in there, any one of them could be failing.

  • make the filename, assign it to a string, print that out
  • load the text asset, print out its contents
  • deserialize it, see if you get a non-null object
  • look inside the object, are the fields what you expect?

Only do ONE THING AT A TIME if you are unsure if it is going to work.

may i ask how i break this apart? xd

A great place to start would be to look at the list of four things I identified above and then understand which portions of that line of code do what of those four things (it’s not a trick question, they’re really what that code is doing!), and thus learn a little bit about how things are put together in your game.

This forum is not really so much for typing in other people’s code without understanding it, then having other people fix it for you. If you found some code and it doesn’t work, great, post here, but you have to at least make a token effort to understanding what you are looking at, and it doesn’t get a whole lot simpler than the first problem I broke down near the top of the post, or the second one I broke down above.