GameObject.Find() not working on client.

I am making a simple networking game. The scene has a button named ‘ReloadButton’ and i am trying to find this button and add a listener to it through my script attached on the player.

private Button reloadBtn;

void Start()
    {
        GameObject tempGO = GameObject.Find("ReloadButton");

        if (tempGO != null)
        {
            reloadBtn = tempGO.GetComponent<Button>();
            reloadBtn.onClick.AddListener(weaponManager.Reload);
        }
    }

I am doing it this way because direct referencing of ‘ReloadButton’ to the script through public Buttonvariable is not possible.

The code works fine on the server, the listener is also added correctly. but on the client, the GameObject.Find("ReloadButton") throws a NullReferenceException.

Seems like the client cannot find the Button itself.
I cannot proceed further in my project without solving this problem, I hope some of you can point me towards the problem.

hi
how about giving it a special tag and search with tag or any other component that are attached to the same object like this :

GameObject.FindObjectOfType<Button>();

I solved it, I made a foolish mistake…In my update method i was disabling this button component from server, So as i was disabling the button from server, it also got disabled on all the clients and was inaccessible.

void Update()
{
        if (reloadBtn != null)
        {
            if (currentWeapon.bullets < currentWeapon.maxBullets)
                reloadBtn.gameObject.SetActive(true);
            else
                reloadBtn.gameObject.SetActive(false);
        }
}

So before the if condition i just added another condition if(isLocalPlayer), to ensure that the code runs only on local client and is working as expected.

Thank you @Cuttlas-U for the reply :slight_smile: