- Home /
Object reference exists but also doesn't?
Note: I'm not sure if this is actually a GUI object issue or if it's an issue with objects in general. This is the first time I've seen anything like this and have no idea what to even think or do about it. If it's something unrelated to GUI specifically and has another reason for happening, please help me find out what's up.
I'm working on a party member display screen for an RPG I've been making. Things were going pretty smooth until I came to a jarring halt with this... whatever is happening.
A quick overview of how this works. The MenuParty script takes GUI text objects and assigns party information from a loaded CharacterData class. All the loading works perfectly fine. This shot shows that I've connected the references, and what the menu initially looks like before being overwritten with character data. Green lines are the ones that work fine. The red one however, inexplicably doesn't.
This shows a bulk of the information. Stats and icons and things are written perfectly fine without a complaint. The TextCharacterOverview object is treated no differently from TextCharacterStats or other text stuff at all. Yet for no reason I can see, it will not be actually found despite having its reference directly assigned to PanelCharacter, which has the script that writes the data to the Text objects. There's even a check to ensure that the reference textOverview isn't null. It even treats it as indeed existing, then immediately throws the NullReferenceException implying textOverview hasn't been linked. The else of that checker is to say "What the hell" if textOverview doesn't exist, but this code is never reached.
I've tried deleting that Text object and using a new one, rolled back my project to before I started doing this and reattempted to create and link the text (same thing occurs), and probably a couple other fix attempts that simply did not change it at all.
I can also mention that there isn't any other objects that are even capable of interfering with these menu things. Just a singleton where the CharacterData comes from (functions perfectly) and WorldManager that lets you return to the map you're currently on (functions perfectly). Neither do anything that can interfere with text or these objects directly. The full script to what goes on in the MenuParty.cs script is here, where all the manipulation occurs.
http://pastebin.com/w3B4iFw2 - using pastebin to not clog the post a bunch. I hope that's okay? DisplayStats() and DisplayOverview() are pretty similar, so the fact one works and the other just doesn't want to make me pretty sure there's something outside of just my code that is the problem.
I realize this isn't much to go by. What else could I be missing (it's probably staring me in the face)? Why does a reference exist then not exist when it must be written to? When is my hair going to turn grey? Thanks for reading through, and double so if you can help point me in the right direction. I'm having another instance of this occur in my project and they're going to be the death of me.
A shot in the dark but did you check if your CharacterData read is null?
Answer by Baste · Jun 18, 2015 at 02:37 PM
It's not the textOverview object that's null, it's the "read" object.
When you combine an assignment over several lines, the first line will show up as the one throwing the error, even if the nullreference is comming from a later line. Here's an example
void Start() {
string someText = "lol";
string someOtherText = null;
string testText = //the error will show up on this line
"the first text has the length " + someText.Length +
"\nthe second text has the length " + someOtherText.Length; //the error is caused here
Debug.Log(testText);
}
So either the read itself is null, or it's one of the indices of read.actions that is null. Hope that helps!
addendum: I'm pretty sure that this is because the compiler turns multiline assignments like that into one line, which means that the runtime can't know which of the lines that's causing the error.
This helped a lot, yeah. I mistakingly assumed that the actions would simply return null and Unity would perform black magic to just make it not add anything to the full string. Turned out that actions being null were the problem. I'll have to add a handle for it after all.