Unity3D按钮在尝试禁用时显示为空

时间:2018-05-29 08:40:56

标签: c# unity3d scripting

我要做的是在时间到时禁用按钮。但是当我将按钮设置为禁用时出现错误:它似乎为空。

以下脚本( RightButton.cs )包含一个名为DisableButton的函数,该函数由主脚本( TappingEngine.cs )调用。

在DisableButton函数中挂起的语句是btn.interactable = false;

/* RightButton.cs */

using UnityEngine;
using UnityEngine.UI;

public class RightButton : MonoBehaviour {

    Button btn;

    // Use this for initialization
    void Start () {
        Debug.Log("Right button ready.");
    }

    public void Tap()
    {
        Debug.Log("Tap right! tot: " + TappingEngine.te.nTaps);
        TappingEngine.te.Tap(TappingEngine.tapType_t.right);
    }

    public void DisableButton()
    {
        btn = this.GetComponent<Button>();
        btn.interactable = false;
    }
}

以下代码是 主脚本的一部分(很长)。但是,有人建议我发布完整版本以保持清晰。

(专注于两个功能Update ()DisableButtons()。)

/* TappingEngine.cs */

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


public class TappingEngine : MonoBehaviour {


    public static TappingEngine te;
    public int nTaps = 0;
    public int GlobalTotal;
    public int RightTaps;
    public int LeftTaps;
    public string FirstTap;
    public int LastTap;
    public int MaxChain;
    public int FastestTwoTaps;
    public int FastestTwoChainedTaps;

    public GlobalTotalTaps GlobalTotalTaps_script;
    public TotalRightTaps RightTotalTaps_script;
    public TotalLeftTaps LeftTotalTaps_script;
    public FirstTap FirstTap_script;
    public RightButton RightButton_script;

    const float TimeSpan = 5F;
    public float Chrono;
    public bool ChronoStart = false;
    public bool ChronoFinished = false;
    public float ElapsedPercent = 0F;

    public enum tapType_t { right, left };

    // Use this for initialization
    void Start () {
        GlobalTotal = 0;
        te = this;
        GlobalTotalTaps_script = FindObjectOfType(typeof(GlobalTotalTaps)) as GlobalTotalTaps;
        RightTotalTaps_script = FindObjectOfType(typeof(TotalRightTaps)) as TotalRightTaps;
        LeftTotalTaps_script = FindObjectOfType(typeof(TotalLeftTaps)) as TotalLeftTaps;
        FirstTap_script = FindObjectOfType(typeof(FirstTap)) as FirstTap;
        RightButton_script = FindObjectOfType(typeof(RightButton)) as RightButton;
    }

    // Update is called once per frame
    void Update () {
        if(ChronoStart) {

            ElapsedPercent = (Time.time - Chrono) * 100 / TimeSpan;

            if(ElapsedPercent >= 100F) {
                DisableButtons();
                //SceneManager.LoadScene("Conclusion", LoadSceneMode.Single);
            }
        }
        Debug.Log("Elapsed time: " + (Time.time - Chrono));
    }

    private void DisableButtons()
    {
        RightButton_script.DisableButton();
    }

    public void OnTap() {
        Debug.Log("Tap!");
    }

    public void Tap(tapType_t tapType) {

        Manage_GlobalTotalTaps();
        if(GlobalTotal == 1) {
            Manage_FirstTap(tapType);
            ChronoStart = true;
            Chrono = Time.time;
        }
        Manage_LeftRight(tapType);


    }

    private void Manage_FirstTap(tapType_t tapType)
    {
        FirstTap = tapType.ToString();
        FirstTap_script.FastUpdate();
    }

    private void Manage_LeftRight(tapType_t tapType)
    {
        if (tapType.Equals(tapType_t.left))
        {
            LeftTaps++;
            LeftTotalTaps_script.FastUpdate();
        }
        else
        {
            RightTaps++;
            RightTotalTaps_script.FastUpdate();
        }
    }

    public void Manage_GlobalTotalTaps() {
        GlobalTotal++;
        GlobalTotalTaps_script.FastUpdate();
    }
}

下面是一些我希望有用的截图。

enter image description here enter image description here

到目前为止,我还没有弄清楚问题出在哪里。此外,该按钮的脚本与表示统计信息的各种Text对象(屏幕右侧的列表)非常相似。

使用Text个对象时,Button对象不可用。

1 个答案:

答案 0 :(得分:0)

我在Unity3D论坛上问了同样的问题,我得到了解决方案here

在我附加RightButton.cs的游戏对象中,第24行Button btn;上有一个变量需要设置为public,所以public Button btn;

这使得在右键对象的Inspector中出现一个新条目(见下图),因此在Unity3D中将右键GameObject 拖放到< strong>右键(脚本)的正确字段(在这种情况下为 Btn )。

enter image description here