Weird bug in Unity

That line (and similar):

this.gameObject.GetComponent<InputField>().text = levObjData.triggerParam6;

produces following error in InputField:

NullReferenceException: Object reference not set to an instance of an object
UnityEngine.UI.InputField.set_text (System.String value) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/InputField.cs:273)
AdvancedUser.Update () (at Assets/code/leveleditor/AdvancedUser.cs:54)

Yes, triggerParam6 is a string, it exists, and it’s inside a struct (levObjectData). Welp. No idea how to get past that.

I am on the verge of reporting it, but I don’t know if it is a known issue (no reason to report it then) and if there is any workaround to make it work.

I don’t think it’s an issue with triggerParam6.

I think GetComponent() is returning null.

Are you sure the component is attached to your game object?

1 Like

Yes, 100% sure.

Take out this.gameObject.. That’s a bit redundant. Probably won’t fix your problem though. Can we know a bit more about the script? Is it a base class? Is the class static? What are you trying to do? Is the script component properly setup on the game object. Also it is possible that levObjData is null so check that.

Ya either InputField is not set, of levObjData is null. I have a rule on GetComponent for any object that isn’t on a [RequiredComponent]. Always set it, and check if it’s null before accessing. That extra 4 seconds of typing saves me several minutes of debugging every time.

1 Like

Numbered for your and mine convenience.

  1. No, it’s just standard MonoBehaviour. If you mean levObjData, that’s just a struct. as in “public struct”.
  2. n/a, see above.
  3. Trying to set inputfield’s text, duh.
  4. 100% certain of that. Also I have bunch of other input fields managed by that script that set text of other fields based on certain values and these work fine.
  5. Nope, works fine in other cases.

Sigh. I never knew I’d have to do that, but here’s the script in its whole gloomy, TheDailyWTF-y glory: http://pastebin.com/2FnQFVan

Just for the sake of knowing that nothing is null, in your start function (or anything), do this:

if (var == null)
    Debug.Log("Var is null");

Do this for every variable you have in the script. That means even the variables in the update function. (temporarily move the get component calls to the start function just to test the variables)

Never ever say “But it works fine in other cases”. Oh my goodness I can’t tell you how many times I’ve thought that and been dead wrong. Put some Debug.Log statements in there and actually check if the objects are null. I think you’ll be surprised.

3 Likes

Not 100% sure as I’m on my phone not at my pc but I think it’s because you don’t have a label assigned for the input…but I could be wrong.

You don’t get this… NRE happens in, and I quote, “UnityEngine.UI.InputField.set_text (System.String value) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/InputField.cs:273)”, therefore it is a problem with Unity’s UI code, not mine.

Actual problem that happens at runtime and stops the game.

heh. I deleted my post and was going to rewrite it when I got home from work but you beat me to it and quoted me before I could.

Go to your input field component in the inspector. Make sure all of the references there are set up correctly. Input fields rely on several Text components found on other GameObjects.

1 Like

This. And if you check it and there are no obvious problems, try deleting and re-creating the element. If you duplicated it from another object, it could be wonky. I have encountered this recently where UI and anim components will behave oddly after duplicating, and recreating it solves the problems. (they weren’t getting unique GUIDs for some reason). It appears to be an editor bug, but I can repro it consistently.

Why don’t you just reference a public InputField, drag and drop or FIND the input field, then go down one line and do NameOfInputField.text = so and so. That’s what I do and IT DOES WORK.

2 Likes

True, the code is intensely overkill, but it still should work as expects unless some has run afoul on the gameObject side.

Uhm is it because you need to ToString() behind triggerParam4-6 ?? :slight_smile: not an expert but that’s something that stood out to me :slight_smile:

Didn’t realize using at least a drag and drop or find the input field only once was that bad of a overkill. Guess I learn something new every day lol.

Yeah I noticed the .ToString() part as well, just wasn’t sure what his variable was “levObjData.triggerParam6”

1 Like

Well it doesn’t look to be a string so that’s what I thought :slight_smile:

1 Like

He says that it is a string in the original post.