Button won't show up (via script)

Hey guys.

My mate and i we are in pretty early stage of our project. We gonna try tochange our menu by script but it doesnt work the way we want it to.

About the script:
I’ve just added the function which provides us with the problem. We open a panel where we can buy our factory depending on which profession we chosen before (case 1 and 2). As we got 500 money we are able to click the button and buy the factory “Button_Betrieb_Kaufen”.
We deactivate the button and want another one to appear on the same spot - its the “Button_Betrieb_Arbeiten” Button, but it won’t show up at all.
I thought it could be a problem to do it by GameObject.Find so i also tried it by GetComponent with a var GameObject for both buttons to get a clear reference.

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

public class CityFunction : MonoBehaviour
{
    //  public Text CharName;
    //  private int Profession;

    [SerializeField]
    private Text CharMoneyText = null;

    private Dictionary<string, int> Produkte = new Dictionary<string, int>();
    private int CharMoneyInt = 0;
    private int WorkInMine = 50;

    public string CharNameTxt;
    public int Vorwahl_Profession;

    public GameObject Panel_Baecker;
    public GameObject Panel_Gerber;

public void Buy()
    {
        if (CharMoneyInt >= 500)
        {
            switch (Vorwahl_Profession)
            {
                case (1):
                    Debug.Log(CharMoneyInt);
                    CharMoneyInt = CharMoneyInt - 500;
                    GlobalControl.Instance.CharMoney = CharMoneyInt;
                    GameObject.Find("Button_Betrieb_Kaufen").SetActive(false);
                    GameObject.Find("Button_Betrieb_Arbeiten").SetActive(true);
                    GameObject.Find("Kosten").SetActive(false);           
                    GameObject.Find("Produkt").SetActive(true);

                    CharMoneyText.text = CharMoneyInt + " Taler";
                    break;
                case (2):
                    CharMoneyInt = CharMoneyInt - 500;
                    GlobalControl.Instance.CharMoney = CharMoneyInt;
                    GameObject.Find("Button_Betrieb_Kaufen").SetActive(false);
                    GameObject.Find("Kosten").SetActive(false);
                    GameObject.Find("Button_Betrieb_Arbeiten").SetActive(true);
                    GameObject.Find("Produkt").SetActive(true);
                    CharMoneyText.text = CharMoneyInt + " Taler";
                    break;
                default:
                   
                    break;
            }
        }
    }

I’ve just added those small part where we have the problem - guess the other functions in here shouldnt make any troubles with it.
And of course - i found another solution for it. But it is just some kind of “do it another way because you dont understand it”, BUT i want to understand why it doesnt work that way like i mentioned in this code.

Hopefully anybody can help us to get an idea about whats wrong - and sorry our variables are both englisch and german mixed ^^

Greetings
Terotox

I’m not a fan of string lookups as it only takes someone renaming a gameobject to break your code without and compiler errors. You may be better off having public variables and assigning them in the inspector or perhaps just having the one button and changing which function runs when it is clicked depending on the current game status.

However, for now, you could just make some private variables and look for the object in the start method and log an error if they are not found. Like so;

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

public class CityFunction : MonoBehaviour
{
    //  public Text CharName;
    //  private int Profession;

    [SerializeField]
    private Text CharMoneyText = null;

    private Dictionary<string, int> Produkte = new Dictionary<string, int>();
    private int CharMoneyInt = 0;
    private int WorkInMine = 50;

    public string CharNameTxt;
    public int Vorwahl_Profession;

    public GameObject Panel_Baecker;
    public GameObject Panel_Gerber;

    private List<GameObject> _targets;

    private GameObject _button_Betrieb_Kaufen;
    private GameObject _button_Betrieb_Arbeiten;
    private GameObject _kosten;
    private GameObject _produkt;

    public void Start()
    {
        _button_Betrieb_Kaufen = GameObject.Find("Button_Betrieb_Kaufen");

        if (_button_Betrieb_Kaufen == null)
            Debug.LogError("_button_Betrieb_Kaufen not found");

        _button_Betrieb_Arbeiten = GameObject.Find("Button_Betrieb_Arbeiten");

        if (_button_Betrieb_Arbeiten == null)
            Debug.LogError("_button_Betrieb_Arbeiten not found");

        _kosten = GameObject.Find("Kosten");

        if (_kosten == null)
            Debug.LogError("_kosten not found");


        _produkt = GameObject.Find("Produkt");

        if (_produkt == null)
            Debug.LogError("_produkt not found");
    }

    public void Buy()
    {
        if (CharMoneyInt >= 500)
        {
            switch (Vorwahl_Profession)
            {
                case (1):
                    Debug.Log(CharMoneyInt);
                    CharMoneyInt = CharMoneyInt - 500;
                    GlobalControl.Instance.CharMoney = CharMoneyInt;
                    _button_Betrieb_Kaufen.SetActive(false);
                    _button_Betrieb_Arbeiten.SetActive(true);
                    _kosten.SetActive(false);
                    _produkt(.SetActive(true);

                    CharMoneyText.text = CharMoneyInt + " Taler";
                    break;
                case (2):
                    CharMoneyInt = CharMoneyInt - 500;
                    GlobalControl.Instance.CharMoney = CharMoneyInt;
                    _button_Betrieb_Kaufen.SetActive(false);
                    _kosten.SetActive(false);
                    _button_Betrieb_Kaufen.SetActive(true);
                    _produkt.SetActive(true);
                    CharMoneyText.text = CharMoneyInt + " Taler";
                    break;
                default:

                    break;
            }
        }
    }
}

You would need the objects to be active on start and disable the ones you do not need once you have found them as gameobject.find will not find an inactive object.

My first thought was the second button is disabled at the start. there for the Find will not find it. and it should through an error when you try the enable it.

i love all the null ref checks that WarMedx is doing.

I feel pretty stupid now… ^^
Thanks for the solution and advice guys, you are great!

I will remind the way you gonna check for gameobjects Warmedxmints. Seems like a pretty nice way to search for errors:)

Indeed we had our 2nd button disabled through the inspector>_<

Thanks and have a nice evening o/