How to controll external buttons from main button with script

So basically, I am working on a racing game, and now I started with the main menu. I’d like to click on “New Game” and then enable the other one that is called “Select your Car”. My code is:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class NewGameBehavior : MonoBehaviour
{
    public Button yourButton;

    void Start()
    {
        Button btn = yourButton.GetComponent<Button>();
        btn.onClick.AddListener(TaskOnClick);
    }


    void TaskOnClick()
    {
        GameObject[] configuration = GameObject.FindGameObjectsWithTag("Button");
        foreach(GameObject ob in configuration){
            Button but = ob.GetComponent<Button> ();
            but.gameObject.SetActive (false);
        }

        GameObject[] selectButtons = GameObject.FindGameObjectsWithTag("SelectCarButton");
        foreach(GameObject ob in selectButtons){
            Button but = ob.GetComponent<Button> ();
            but.enabled = true;
            but.interactable = true;
        }

        Camera.main.transform.Translate (1000, 0 , 0);
        Debug.Log("You have clicked the button!");
        yourButton.gameObject.SetActive(false);

    }
}

Also, here is SelectCarButton:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;


public class SelectCarButton : MonoBehaviour {

    // Use this for initialization
    public Button select;

    void Start()
    {
        Button bt = select.GetComponent<Button>();
        bt.gameObject.SetActive (false);
    }
    // Update is called once per frame
    void Update () {
       
    }

    void Enable(){
        //enabled = true;
    }
}

So, what I’m trying to do is to disable New Game Button (“button” tag), so it is no longer on screen, and then enable “Select Car Button” (“SelectCarButton” tag): Disabling works, but I can not enable the “Select Car Button” with this code.

Is there any way to enable external buttons from this script?

Let’s say you make 1 script. Inside the script you have 2 methods: 1 that you want to run when you click the new game button, and 1 that runs when you choose the car. (if that’s 1 button).
Inside the script, you have 2 Button variables.
In the inspector, you add the OnClick events to each button and take the game object with the script mentioned above and drag it into the inspector. You then choose the script → method from the dropdown menu after adding it (inside the OnClick event of the button’s inspector).

Now, in the code, in addition to whatever else you want to do… you have easy access to enable/disable buttons.
Just put this script on some empty game object or something (not on either of the buttons) :slight_smile:

Ok. Now I have this:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class NewGameBehavior : MonoBehaviour
{
    public GameObject newGame;
    public GameObject selectCar;
    public GameObject configuration;

    public void Start(){
        newGame = GameObject.FindGameObjectWithTag("NewGame");
        selectCar = GameObject.FindGameObjectWithTag("SelectCarButton");
        configuration = GameObject.FindGameObjectWithTag("Configuration");
    }

    public void setNewGame(){
        Camera.main.transform.Translate (1000, 0 , 0);
        Debug.Log("You have clicked the button!");
        newGame.gameObject.SetActive(false);
        configuration.gameObject.SetActive(false);
        selectCar.gameObject.SetActive (true);
    }

}

It worked just the same as before. But I still don’t know how can I controll Enabling/DIsabling. I tried disabling Select Car button from Start():

public void Start(){
        newGame = GameObject.FindGameObjectWithTag("NewGame");
        selectCar = GameObject.FindGameObjectWithTag("SelectCarButton");
        configuration = GameObject.FindGameObjectWithTag("Configuration");

        selectCar.gameObject.SetActive (false);
    }

But then, when I try to Enable again, I get a NullReferenceException.

Any reason why you can’t just do this using the unity event in the Button component within the inspector?

Are you talking about Event Trigger? It works very similar, so the only problem here is how to refer to other objects.

I’m talking about unity events. You do have the button component on your buttons right? That button component comes with a unity event called onClick. Use the + button in it, drag the object you want into the created tab, access the component and method you want. In this case it would be GameObject>SetActive.

Yeah, that’s what I said. Maybe I’m trying to do something very complex, but I’ll explain it step by step:
1- I’d like to use just one scene for Main Menu, jst beacuase ther will be camera movements.
2- That’s why I have all kind of buttons: New Game, Select Cars, and any other button I choose.
3- If I click a button (New Game in general), then the other buttons dissapear, and other buttons will eventually appear. So, if I click New Game, there should appear Selec Car.

OnClick() works perfectly on Disabling New Game Button after clicking, but I can’t Enable Select Car due to these problems:
1- I have to initiate Select Car as a disabled button.
2- If I try to Enable it, I get a NullReferenceException.

Then I just have to solve that problem: Enabling an object previously Disabled

so what’s wrong with doing that in the inspector like this?

The problem is that I don’t know how to enable other elements properly.
Since I have to click just one button at time, I expect this event to enable and disable at the same time and place.
But Disabled elements are considered as nulls. So, is there another way to set disabled elements at the begining and then call them? (Take my last code as the OnClick method)

There’s a + button there to add more tabs to do more things under the same event. Just do the same for any buttons that are supposed to appear or dissapear. Everything within the event will run the moment the button holding the event is clicked.

Hopefully this is working out :slight_smile:

Yeah. It finally worked! :smile: Thanks guys.

Good to hear :slight_smile: