Search code examples
c#unity-game-engineinput-field

Input Field doesn't react in Unity with C#


I'm not able to get information from my Input Field - I'm even unable to type in a value. Input- Field - Settings

The code is:

using UnityEngine;
using UnityEngine.UI;

public class ButtonPress : MonoBehaviour {

    public InputField GrasInput;
    public Button playbutton;
    int GrasVal=0;

    private void Start()
    {
        Button btn = playbutton.GetComponent<Button>();
        btn.onClick.AddListener(TaskOnClick);
    }

    void TaskOnClick()
    {
        GrasVal++;
        GrasInput.text = GrasVal.ToString();
    }
}

What am I doing wrong, I wasn't able to find a solution, tried different things to solve problem (not mentioned in the code above, because none of them worked out)? All of the components (GrasInput and playbutton) are connected in the console via the _Manager and this script. (The whole project is uploadet on http://simsoeko.tk/)

Thanks and sorry, I'm just a beginner and really tried to find out why it won't work.


Solution

  • Looking at the screenshot provided, your Text GameObjects are further down in the hierarchy, also their size seems to "contain" the inputfields, therefor they are blocking the raycasts to your InputFields

    Either detick Raycast Target on your Text objects.

    Or move the InputFields further down in the hierarchy (while remaining children of Canvas)