Lukas
Lukas

Reputation: 437

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.

Upvotes: 0

Views: 5440

Answers (2)

jhchnc
jhchnc

Reputation: 459

Maybe your scene is missing an EventSystem object. Add it under Hierarchy > create > UI > EventSystem (https://docs.unity3d.com/ScriptReference/EventSystems.EventSystem.html)

per: https://answers.unity.com/questions/1540770/cant-click-on-input-fields.html

Upvotes: 0

Fredrik Widerberg
Fredrik Widerberg

Reputation: 3108

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)

Upvotes: 3

Related Questions