UI Button Controls

Hi i was wondering how would i make it so that i could use the UI Button to switch weapons (Since I’m developing for android). Here’s the script here :

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

public class SwitchWeaponsScript : MonoBehaviour
{

    public GameObject weapon1;
    public GameObject weapon2;




    void Update(GameObject SwitchWeaponButton)
    {
        if (SwitchWeaponButton.GetButtonDown)
        {
            switchWeapon();
        }
    }

    void switchWeapon()
    {
        if (weapon1.activeSelf)
        {
            weapon1.SetActive(false);
            weapon2.SetActive(true);
        }
        else
        {
            weapon1.SetActive(true);
            weapon2.SetActive(false);
        }
    }
}

I don’t understand if i should use GetButtonDown, GetButtonUp etc. Please Help

Thanks in advance

Make the switchWeapon() function public and link it to your UI button so whenever you press the button, switchWeapon() will be called.

2 Likes

You don’t need that part in update and as gamer94 said create in editor button and i OnClick() function (right in inspector of button) drag game object to field and select SwtichWeaponsScript > switchWeapon (it has to be public)

if need more help there are many video tutorials for this

1 Like

Lol I didn’t think of that thanks