简体   繁体   English

Unity 3D 警告 CS0414:该字段<field>已分配但从未使用其值

[英]Unity 3D warning CS0414: The field <field> is assigned but its value is never used

I'd like to know why this error happens in the code below.我想知道为什么在下面的代码中会发生此错误。

I assigned the correspondent GO's (Game Objects) in the inspector.我在检查员中分配了通讯员 GO(游戏对象)。

PhotonNetwork.ConnectUsingSettings, OnConnectedToMaster, OnJoinedLobby working ok PhotonNetwork.ConnectUsingSettings, OnConnectedToMaster, OnJoinedLobby 工作正常

The method On_PlayerNameInput_changed() already assigned to PlayerName InputField in hierarchy (and PlayerName InputField as PlayerNameScreen 's child).该方法On_PlayerNameInput_changed()已经分配给PlayerName InputField在层次结构(和PlayerName InputFieldPlayerNameScreen的孩子)。

GO's attached to MenuManager GO slots GO 附加到 MenuManager GO 插槽

Now, I have this code现在,我有这个代码

public class MenuManager : MonoBehaviourPunCallbacks
    {
    [SerializeField] private GameObject connectScreen, playerNameScreen, playerNameButton; 

    [SerializeField] private InputField createRoomInput, joinRoomInput, playerNameInput; 

    public void OnClick_PlayerNameButton() 
    {
        PhotonNetwork.NickName = playerNameInput.text; 
    }

    public void On_PlayerNameInput_changed() 
    {
        if (playerNameInput.text.Length > 2 && playerNameInput.text.Length < 11)
            playerNameButton.SetActive(true);

        else
            playerNameButton.SetActive(false);
     }

And no warning is shown, but if I do the next:并且没有显示警告,但是如果我执行以下操作:

public void On_PlayerNameInput_changed() 
    {
        if (playerNameInput != null)
        {
            if (playerNameInput.text.Length > 2 && playerNameInput.text.Length < 11)
                playerNameButton.SetActive(true);

            else
                playerNameButton.SetActive(false);
        }
        else
            Debug.Log("PlayerNameInput is null");
    }

Immediately appear the next warning messages:立即出现下一个警告信息:

Assets\\Scripts\\MenuManager.cs(11,57): warning CS0649: Field 'MenuManager.playerNameButton' is never assigned to, and will always have its default value null Assets\\Scripts\\MenuManager.cs(11,57):警告 CS0649:字段“MenuManager.playerNameButton”从未分配给,并且其默认值始终为空

Assets\\Scripts\\MenuManager.cs(14,56): warning CS0649: Field 'MenuManager.playerNameInput' is never assigned to, and will always have its default value null Assets\\Scripts\\MenuManager.cs(14,56): 警告 CS0649: 字段 'MenuManager.playerNameInput' 从未分配给,并且其默认值始终为 null

Assets\\Scripts\\MenuManager.cs(11,39): warning CS0649: Field 'MenuManager.playerNameScreen' is never assigned to, and will always have its default value null Assets\\Scripts\\MenuManager.cs(11,39): 警告 CS0649: 字段 'MenuManager.playerNameScreen' 从未分配给,并且其默认值始终为 null

And as shown in the picture, the GO's are already dragged and dropped in the MenuManager slots.如图所示,GO 已经被拖放到 MenuManager 插槽中。

Thanks for any help.谢谢你的帮助。

As it says, you have created symbols which are always going to be null.正如它所说,您已经创建了始终为空的符号。 There is nothing here that would ever assign an instance of an object to the symbols mentioned in the warnings.这里没有任何东西可以将对象的实例分配给警告中提到的符号。

You need to either construct objects and assign them to those symbols before you try to use them, or you can make the fields public and assign a constructed object from the outside.在尝试使用它们之前,您需要构造对象并将它们分配给这些符号,或者您可以公开字段并从外部分配构造的对象。 As it is, the compiler correctly assumes that a declared private symbol that is never assigned to a constructed object is going to throw a null-reference exception at runtime when you try to operate on it.实际上,编译器正确地假定,当您尝试对其进行操作时,从未分配给构造对象的已声明私有符号将在运行时抛出空引用异常。

If you don't yet understand the concepts of reference and instance in C#, then that's the concept to read up on.如果您还不了解 C# 中引用和实例的概念,那么这就是要阅读的概念。 A symbol is always "null" unless something creates, then assigns, a runtime instance of an object.符号始终为“空”,除非创建并分配对象的运行时实例。

Visual Studio doesn't know that the SerializeField members are set by the Unity Editor. Visual Studio 不知道SerializeField成员是由 Unity 编辑器设置的。 If you initialize them with default , that removes the warning.如果您使用default初始化它们,则会删除警告。

[SerializeField] private GameObject connectScreen, playerNameScreen, playerNameButton = default;

[SerializeField] private InputField createRoomInput, joinRoomInput, playerNameInput = default;

This describes how you can quickly do that for your whole project.描述了如何为整个项目快速执行此操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 该字段永远不会分配给它,并且其默认值始终为null - The Field Is Never Assigned To And Will Always Have Its Default Value null 私有类中的公共字段:“Field [FieldName]永远不会分配给,并且将始终具有其默认值null” - Public field in private class: “Field [FieldName] is never assigned to, and will always have its default value null” 如何修复“字段 X 从未分配给并且其默认值为空”? - How to fix "Field X is never assigned to and will have its default value null"? 字段&#39;XXX.AppMain.p1&#39;从未分配,并且其默认值始终为空 - Field 'XXX.AppMain.p1' is Never Assigned to, And Will Always Have Its Default Value Null 从未将struct的列表对象分配给它,并且将始终具有其默认值null - list object of struct is never assigned to and will always have its default value null Java“null”值从未分配给我的Runnable - Java “null” value never assigned to my Runnable Love2d-尝试索引字段“文件系统”(nil值) - Love2d- attempt to index field 'filesystem' (a nil value) 如何确保Java类中的字段永远不会为null - How to ensure that a field will never be null in a Java class 在选择格式为d.value的固定字段名称时避免D3.js“ groupData未定义”错误 - Avoiding D3.js “groupData is undefined” errors when selecting on fixed field name of form d.value 在构造函数中,指定的最终对象字段是否仍然为空? - Is it true that the assigned final object field may still be null inside a constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM