0
\$\begingroup\$

So I'm trying to get a script of mine to pass information to another static class being used for saving files as I've been told I wouldn't want that script to be instantiated. So I used a separate script to get input field text. However, I seem to be encountering a problem whenever I want to save the file.

I keep getting the error "Object reference not set to an instance of the object", I have made done this though. (See code below)

The class to get the name of the file

public class SaveInputField : MonoBehaviour
{
    public static string saveName;
    public GameObject inputField;



    public void getFileName(string saveName)
    {

        saveName = inputField.GetComponent<Text>().text;
        Debug.Log("Save file name: " + saveName);
        SaveFile.saveName = saveName;
    }
}

^called whenever its been edited

The static class

public static class SaveFile
{

    public static string saveName;

    public static void SaveData(Deck deck)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        string path = Application.persistentDataPath + "/" + saveName + ".dd";

        FileStream stream = new FileStream(path, FileMode.Create);
        Debug.Log("Stream Opened");
        DeckData data = new DeckData(deck);

        formatter.Serialize(stream, data);
        stream.Close();
        Debug.Log("Stream Closed");

    }
```
\$\endgroup\$
4
  • \$\begingroup\$ Where does this give you the "Object reference" crash? \$\endgroup\$ – Tordin Dec 12 '19 at 19:10
  • \$\begingroup\$ saveName = inputField.GetComponent<Input>().text; Sorry I forgot about that part \$\endgroup\$ – Kiyo Dec 12 '19 at 19:23
  • \$\begingroup\$ Cross-site duplicate. \$\endgroup\$ – Draco18s no longer trusts SE Dec 12 '19 at 20:12
  • \$\begingroup\$ @Kiyo That means either inputField is null or .GetComponent<Input>() is null. \$\endgroup\$ – Nick May 11 '20 at 5:57
1
\$\begingroup\$

Here you ask for a game object:

public GameObject inputField;

But what you actually use is a text component:

saveName = inputField.GetComponent<Text>().text;

This gives multiple potential points of failure: either you forget to set the inputField in the inspector, or you set it, but the game object you set doesn't have a Text component (or the Text component is in one of its children), or the reference had what you need, but it got unloaded/destroyed.

If what you want is a Text component, you can just ask for that in the first place.

public Text inputText;

...

if(inputText == null) Debug.LogError("Remember to assign your input text!");
saveName = inputText.text;

For starters this is more efficient, since it saves searching for the component via GetComponent at runtime. But it also gets us some reliability improvements too.

Now the inspector will enforce that you reference an object that actually has a text component, reducing the number of potential failure modes and opportunities for mistakes. This can still fail if you forget to wire it up entirely, or destroy/unload the object that's being referenced, but at least now you get an error message to remind you what you did wrong. ;)

\$\endgroup\$
0
\$\begingroup\$

In cases where you have crashes in MonoBehaviours, i find it the best way to debug them to split each line for null checks. (absolutely slower but efficient for debugging)

For your code, you are having a crash in a particular line, if you debug that exact line you will see what will be null. But due to your compactly written code this can be somewhat hard.

I would recommend to rewrite your code into something like this :

if( inputField == null )
return;
Input inputComponent = inputField.GetComponent<Input>();
if( inputComponent == null )
return;
string saveNameFromComponent = inputComponent.text;
\$\endgroup\$
6
  • \$\begingroup\$ whats with the empty returns? \$\endgroup\$ – Kiyo Dec 12 '19 at 20:41
  • \$\begingroup\$ Also, inputComponent.text doesnt work because thats apparently not a method of inputComponent. \$\endgroup\$ – Kiyo Dec 12 '19 at 20:46
  • 1
    \$\begingroup\$ The empty returns just returns you out of the current function. I usually put my breakpoints there to see at what stage it usually fails. \$\endgroup\$ – Tordin Dec 12 '19 at 22:13
  • \$\begingroup\$ In such a case, nothing got returned, and I still got the error. Thanks for getting back to me but looks like I still need something to change... Any other suggestions? It fails on the same line where im trying to fetch text. Do you think it could be because im using TextMeshPro and not the default? \$\endgroup\$ – Kiyo Dec 12 '19 at 22:36
  • \$\begingroup\$ if it fails on that sime line, you might have missed to connect the needed component in the prefab editor. \$\endgroup\$ – Tordin Dec 13 '19 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.