简体   繁体   中英

An Unity3D button appears to be null while trying to disable it

What I'm trying to do is disable the button when the time is up. But I get an error while I set the button to be disable: it appears to be null.

The following script ( RightButton.cs ) includes a function called DisableButton that is invoked by the main script ( TappingEngine.cs ).

The statement that hangs inside the DisableButton function is 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;
    }
}

The following code is a part of the main script (which is quite long). However, someone advised me to post the full version for clarity.

(Focus on the two functions Update () and 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();
    }
}

Below are a couple of screenshots that I hope will be useful.

在此处输入图片说明 在此处输入图片说明

Until now I have not yet figured out where the problem is. Moreover, the script of the button is very similar to that of the various Text objects that represent the statistics (the list on the right side of the screen).

With Text objects it works, with the Button object not.

I asked the same question on Unity3D Forum and I got the solution here .

One the game object where I attached the RightButton.cs there is a variable on line 24 Button btn; that needs to be set to public , so public Button btn; .

This makes appearing a new entry in the Inspector of the Right Button object (see the image below) and so in Unity3D drag and drop the Right Button GameObject into Right Button (Script) 's proper field ( Btn in this case).

在此处输入图片说明

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM