NullReferenceException when trying to get text from inputfield - text

I am new to unity and want to get a value from a input text field. I found this question
Get text from Input field in Unity3D with C#, but when I execute it the same error always appear: NullReferenceExcpetion : Object reference not set to an instance of an object.
It seems like a stupid mistake and I tried everything but can't seem to fix it.
My code:
void Start () {
var input = gameObject.GetComponent<InputField>();
input.onEndEdit.AddListener(SubmitName);
}
private void SubmitName(string arg0)
{
Debug.Log(arg0);
}
I tried putting InputField input; before the start function and erasing var but still no luck.
If anyone can help me with this problem it would be much appreciated.
Pictures of where my scripts are attached at the moment.

Your code is nearly correct, if you have a look at the documentation, you have to call your method with function before calling the method name. It looks to me like you are mixing c# and JS, here is the `js function:
public class Example {
public var mainInputField;
public function Start() {
// Adds a listener to the main input field
// and invokes a method when the value changes.
mainInputField.onValueChange.AddListener(function() {
ValueChangeCheck();
});
}
// Invoked when the value of the text field changes.
public function ValueChangeCheck() {
Debug.Log("Value Changed");
}
}
the c# solution:
public class MenuController : MonoBehaviour {
[SerializeField]
InputField inputText;
// Use this for initialization
void Start () {
inputText.onValueChange.AddListener(delegate {
DebugInput();
});
}
private void DebugInput(){
Debug.Log ("Input: " + inputText);
}
}
My Hierarchie looks like this:
I created a Canvas and inserted an InputField inside it. The script with the code inside is attached to the Canvas and the InputField connected to the [SerializeField] inside the script in the Canvas.
I would recommend you to make your InputField a class variable, so you can access it easier later. Furthermore you should just create a [SerializeField] for your InputField so you can drag it to your script. This might avoid some mistakes too.

Change InputField(TMP) to InputField(Legacy)

Related

In D365/X++, why is '(' invalid when I use "this" to indicate the variable lives on the instance?

I have the following class:
public class MyDialogSelect extends RunBase
{
private DialogField nameField;
// Snipped for brevity
public Object dialog()
{
Dialog dialog = super();
nameField = dialog.addField(extendedTypeStr(CustName));
// Snipped for brevity
return dialog;
}
public void dialogSelectCtrl()
{
CustTable customerTable = CustTable::find(accountField.value());
nameField.value(customerTable.name());
// Snipped for brevity
}
}
This compiles and works as expected.
However, I prefer using the keyword this to indicate when variables belong to the instance, so I try changing it this to:
public class MyDialogSelect extends RunBase
{
private DialogField nameField;
// Snipped for brevity
public Object dialog()
{
Dialog dialog = super();
this.nameField = dialog.addField(extendedTypeStr(CustName));
// Snipped for brevity
return dialog;
}
public void dialogSelectCtrl()
{
CustTable customerTable = CustTable::find(accountField.value());
this.nameField.value(customerTable.name());
// Snipped for brevity
}
}
But, this won't compile, instead resulting in Invalid token '('..
However, if I remove this before nameField.value(customerTable.name());,
it works as expected again. (Note: I still indicate this in this.nameField = dialog.addField(extendedTypeStr(CustName));).
Why won't it compile when I include this before a property which invokes a method?
I've also observed this with this.nameField.enabled(false) also failing.
Is there a more general rule or principle I should understand here about when x++ allows, disallows, or requires this?
You cannot use this to reference instance variables in X++. Like in C++.
You can (and must) use this to refer to instance methods.
This refers to the context of the development, in your example, this refers to the class as a whole. Of you add another method, you would call that method using this.
With latest form pattern changes, I believe the MSFT recommendation is to use dialog form pattern instead of class to generate dialog.

Pass View to method in Acumatica

I ran into such a problem today that I cannot pass the View to the method.
I doing all this in order to get rid of the numerous dubbing code.
For example, I will show how I see it and if there is such the ability to pass to the method, then how to implement it the ability to pass to the method
error when passing to the method
how can I fix this problem?
Try this and see if it gets you anywhere.
public class Test
{
private SelectFrom<Account>.View testView;
public void Test()
{
Method1(testView.View);
Method2(testView);
}
public void Method1(PXView test)
{
}
private void Method2(FbqlSelect<SelectFromBase<Account,TypeArrayOf<IFbqlJoin>.Empty>, Account>.View view)
{
var current = view.Current;
}
}

Binding to Properties of Nested CustomViews in a ViewController

Given I have the following setup (simplified version, removed logic to add to parent view and constraints etc).
public class TestViewModel : MvxViewModel
{
string _text;
public string Text
{
get => _text;
set
{
_text = value;
RaisePropertyChanged(() => Text);
}
}
}
public class TestViewController : MvxViewController<TestViewModel>
{
CustomViewA customViewA;
public TestViewController()
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
var bindingSet = this.CreateBindingSet<TestViewController, TestViewModel>();
bindingSet
.Bind(customViewA)
.For(v => v.Text)
.To(vm => vm.Text);
bindingSet.Apply();
}
}
public class CustomViewA : UIView
{
CustomViewB customViewB;
public string Text
{
get => customViewB.Text;
set => customViewB.Text = value;
}
}
public class CustomViewB : UIView
{
UITextField textField;
public string Text
{
get => textField.Text;
set => textField.Text = value;
}
}
Why is it that the bindings do not work? Only if I would make the UITextField in CustomViewB public and directly bind to it in the ViewController rather than the public property that directs to the Text property it seems to work. Like so:
bindingSet
.Bind(customViewA.customViewB.textField)
.For(v => v.Text)
.To(vm => vm.Text);
What am I missing here?
It depends on the requirements you have.
Binding in one direction should work (view model-to-view), I have tested your code and when the ViewModel property changes, the change is propagated to CustomViewA and from there to CusomViewB and finally to the UITextField.
However, the problem is with the opposite direction (view-to-view model). When the user updates the text field, its Text property changes. However, there is nothing notified about this change.
Although the property Text points to the text field, it is not "bound" to it, so when TextField's Text changes, the property itself doesn't know about it and neither does the MvvmCross binding.
In fact, MvvmCross binding in the control-to-view model direction is based on the ability to observe an event that tells the binding to check the new value of the bining source. This is already implemented for UITextField's Text, and it hooks up the EditingChanged event (see source code).
You can still make custom bindings work in the view-to-view model direction by implementing them manually. This is described in the documentation.

Unity Text Change not working

I have this simple platformer that has coins that you pick up and a canvas that shows the score and changes whenever you pick one up. This is my code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour {
Text text;
private LVLMang levelManager;
void Start () {
text = GetComponent<Text> ();
levelManager = FindObjectOfType<LVLMang> ();
}
void Update () {
text.text = "" + levelManager.Score;
}
}
It will add in the coins to the score, but it gives me so many errors that my game eventually crashes. The error is: 'NullReferenceException: Object reference not set to an instance of an object' and it highlights the text.text line. Please Help. Thanks in advance.
First, make sure you have a Text component on the object that has the script. Your GetComponent<Text> (); call isn't finding a Text component.
Alternatively you can make Text text; public and hook it to the actual Text component you want to change by drag-dropping the Text-component object into the Script component's Text field.
Secondly, after updating the text.text value, call text.SetAllDirty(); to get the change to take effect.

UNITY: Is it possible to access the GameObject that a PropertyAttribute is attached to?

For example if I create a PropertyAttribute;
ExampleAttribute : PropertyAttribute { etc...
and a respective PropertyDrawer
[CustomPropertyDrawer (typeof(ExampleAttribute))]
public class ExampleDrawer : PropertyDrawer
Is it possible to talk to the Monobehaviour script that has invoked the [Example] attribute?
When Unity calls your property drawer's OnGUI, it'll pass a SerializedProperty. The property's serializedObject is the SerializedObject that owns it.
You didn't specify what you wanted to do with that object, but now you have a reference to it:
public void OnGUI(Rect rect, SerializedProperty property, GUIContent label) {
//do other GUI stuff
Debug.Log("I belong to " + property.serializedObject.targetObject, this);
}

Resources