Search Unity

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

Discussion in 'Scripting' started by xXCastigamattiXx, May 29, 2018.

  1. xXCastigamattiXx

    xXCastigamattiXx

    Joined:
    May 7, 2018
    Posts:
    9
    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 <s>a part of</s> 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.
     
  2. Icyteck01

    Icyteck01

    Joined:
    Dec 27, 2016
    Posts:
    31
    That means you have to drag the button in inspector(Editor) where you attached rightbutton.cs
     
  3. xXCastigamattiXx

    xXCastigamattiXx

    Joined:
    May 7, 2018
    Posts:
    9
    It's only recently that I'm using Unity3D. I'm trying to do what you told me, but I'm not succeeding. Can you be more specific please?
     
  4. Icyteck01

    Icyteck01

    Joined:
    Dec 27, 2016
    Posts:
    31
    ok i found to problems.
    One the game object where you attached the RightButton.cs there is a variable on line 24 "Button btn;" that needs to be set to public.. So public Button btn;
    Then in unity drag and drop the game object that RightButton.cs is attached.
     
    xXCastigamattiXx likes this.
  5. xXCastigamattiXx

    xXCastigamattiXx

    Joined:
    May 7, 2018
    Posts:
    9
    Thanks so much! Now the problem is solved and everything works.

    ;)
     
  6. xXCastigamattiXx

    xXCastigamattiXx

    Joined:
    May 7, 2018
    Posts:
    9
    If you are registered to StackOverflow, you can answer the same question I had asked there so I can just mark your answer.
     
  7. Icyteck01

    Icyteck01

    Joined:
    Dec 27, 2016
    Posts:
    31
    not much as a forum guy :D but ty anyway.
     
    xXCastigamattiXx likes this.