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:
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.
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!)
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.