【问题标题】:Disable Button when end of Array is reached到达数组末尾时禁用按钮
【发布时间】:2015-07-24 16:45:30
【问题描述】:

如何正确禁用GameObject 中的按钮?到目前为止,我的代码正在返回错误:

NullReferenceException:对象引用未设置为 对象LoadGallery.nextImage()。

此错误来自以下行:

Button btn = nextBtn.GetComponent<Button>();

我不明白为什么引用无效。还有将 if-else 语句中的重复代码合并到一个位置的最佳做法吗?

// Increment through gallery
if(currentIndexArray < galleryImages.Length)
{
    StartCoroutine("loader", currentIndexArray++);
}
// Disable Next Button when the end is reached
if(currentIndexArray == galleryImages.Length)
{
    GameObject nextBtn = GameObject.FindGameObjectWithTag("NextBtn");
    Button btn = nextBtn.GetComponent<Button>();
    btn.enabled = false;
    btn.interactable = false;
}
else
{
    GameObject nextBtn = GameObject.FindGameObjectWithTag("NextBtn");
    Button btn = nextBtn.GetComponent<Button>();
    btn.enabled = true;
    btn.interactable = true;
}

【问题讨论】:

    标签: c# arrays button unity3d


    【解决方案1】:

    数组索引是零索引的,所以你必须改变

    if(currentIndexArray < galleryImages.Length)
    

    if(currentIndexArray < galleryImages.Length - 1)
    

    if (currentIndexArray == galleryImages.Length)
    

    if (currentIndexArray == galleryImages.Length - 1)
    

    【讨论】:

    • 该编辑似乎不会影响我在禁用按钮时遇到的当前问题。我得到的错误与 btn 的实例有关。
    【解决方案2】:

    在使用 GetComponent&lt;Button&gt;() 方法之前,确保 nextBtngameObject 不为空。

    尝试像这样添加一个空检查:

    GameObject nextBtn = GameObject.FindGameObjectWithTag("NextBtn");
    if(nextBtn != null)
    {
        Button btn = nextBtn.GetComponent<Button>();
        btn.enabled = true;
        btn.interactable = true;
    }
    

    【讨论】:

    • 那是个问题。他们是空的。我把对我有用的解决方案放在上面。
    【解决方案3】:

    我通过公开按钮 gameObjects 让它工作:

    public GameObject nextBtn;
    public GameObject prevBtn;
    

    然后,我将游戏对象放到 Unity 检查器窗口脚本部分的相应字段中。之后,我删除了函数中的 FindObjectByTag 行,其他一切正常。

    public void nextImage()
    {
        // Increment through gallery
        if(currentIndexArray < galleryImages.Length)
        {
            StartCoroutine("loader", currentIndexArray++);
            Debug.Log(currentIndexArray);
        }
        // Disable Next Button when the end is reached
        if(currentIndexArray == galleryImages.Length)
        {
            Button btn = nextBtn.GetComponent<Button>();
            btn.enabled = false;
            btn.interactable = false;
        } else {
            Button btn = nextBtn.GetComponent<Button>();
            btn.enabled = true;
            btn.interactable = true;
        }
    }
    
    public void prevImage()
    {
        // Decrement through gallery
        if(currentIndexArray > 0)
        {
            StartCoroutine("loader", currentIndexArray--);
            Debug.Log(currentIndexArray);
        }
        // Disable Prev Button when the end is reached
        if(currentIndexArray == 0)
        {
            Button btn = nextBtn.GetComponent<Button>();
            btn.enabled = false;
            btn.interactable = false;
        } else {
            Button btn = nextBtn.GetComponent<Button>();
            btn.enabled = true;
            btn.interactable = true;
        }
    }
    

    【讨论】:

    • 可以直接Expose Button而不是GameObject,不需要每次都获取Button Component。编辑答案。
    • 我想我可以从修复中看出是什么导致了较旧的错误:您在调用下一个按钮的“开始”方法之前调用了该方法。所以 find my name 返回 null。
    猜你喜欢
    • 2022-01-05
    • 2018-06-04
    • 2023-02-02
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多