简体   繁体   中英

Unity error CS0246: The type or namespace name 'InputField' could not be found (are you missing a using directive or an assembly reference?)

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class UI : MonoBehaviour
    {
    private static UI _singleton;

    private static UI Singleton
    {
        get => _singleton;
        set
        {
            if (_singleton == null)
                _singleton = value;
            else if (_singleton != value)
            {
                Debug.Log($"{nameof(UI)} instance already exists, destroying duplicate!");
                Destroy(value);
            }
        }
    }

    [Header("Connect")]
    [SerializeField] private GameObject connectUI;
    [SerializeField] private InputField usernameField;

    private void Awake()
    {
        Singleton = this;
    }

    private void ConnectClicked()
    {
        usernameField.interactable = false;
        connectUI.SetActive(false);

        NetworkManager.Singleton.Connect();
    }

    public void BackToMain()
    {
        usernameField.interactable = true;
        connectUI.SetActive(true);
    }

    public void SendName()
    {
        Message message = Message.Create(MessageSendMode.reliable, (ushort)ClientToServerId.name);
        message.AddString(usernameField.text);
        NetworkManager.Singleton.Client.Send(message);
    }    
}

The error:

Unity error CS0246: The type or namespace name 'InputField' could not be found (are you missing a using directive or an assembly reference?)

The error occurs in following line:

[SerializeField] private InputField usernameField;

Tell me how to fix this please?

You need to specify the following using directive in your script, as InputField is part of the UI:

using UnityEngine.UI;

or use

[SerializeField] private UI.InputField usernameField;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question error CS0246: The type or namespace name 'Npgsql' could not be found (are you missing a using directive or an assembly reference?) Error CS0246 The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?) Error CS0246: The type or namespace name 'StreamingContext' could not be found (are you missing a using directive or an assembly reference?) error CS0246: The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?) Error CS0246 The type or namespace name 'CreateRandomAnswersForKey' could not be found (are you missing a using directive or an assembly reference?)? error CS0246: The type or namespace name 'Player' could not be found (are you missing a using directive or an assembly reference?) Unity CS0246: The type or namespace name 'MySql' could not be found (are you missing a using directive or an assembly reference CS0246: The type or namespace name 'Employee' could not be found (are you missing a using directive or an assembly reference?)? CS0246 The type or namespace name 'ErrorViewModel' could not be found (are you missing a using directive or an assembly reference?) C# Error: Error CS0246 The type or namespace name '' could not be found (are you missing a using directive or an assembly reference?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM