Hey all, I’m having some trouble with this concept, and want some help.
I have button on the canvas, and the following onClick script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ButtonTest : MonoBehaviour {
public void touchButton()
{
Debug.Log ("This button is tagged " + EventSystem.current.currentSelectedGameObject.tag );
// more code but removed for brevity
}
}
}
Right now, it just debugs by printing the tag of the button itself. Great, works fine.
I want to then turn the button itself un-interactable, so the players can click on it only once, but I don’t know how to reference the button itself from the script. Any help?
Is that script on the button itself? GetComponent will work only on the current object, unless you call it from another object (for example, OtherGameObject.GetComponent())
You need the script that has the method the buttons call to have a references to the buttons components, so you can use interactable = false.
OR
You can extend the button class and add that functionality to it.
using UnityEngine;
using UnityEngine.UI;
public class OneClickButton : Button
{
public override void OnPointerClick(PointerEventData eventData)
{
base.OnPointerClick(eventData);
interactable = false;
}
}
thanks for your reply, I really really do appreciate it so much. But I read it three times, and I got increasingly confused at what I need to do to get what seemingly would be a simple thing. I’m so so sorry I’m not getting what I need to do to get some code to refer to the button it just pushed.
What does this mean? How do I add what reference to what button component?
Where would I add this bit of code? A separate script? How would I call up this method on the click button?
I am feeling like such an idiot for not getting ANY of this!!!
The pushButton script needs a reference to each button that will have that effect. You need to add those references in that script.
public Button button1;
public Button button2;
public Button button3;
Then in the editor select the object that has the pushButton script, and drag each of the buttons from the hierarchy window into the corresponding slot.
Each button should be calling a separate method, you can then use the reference to “disable” the correct button.
public void Button1Clicked()
{
//Do what you do when button 1 is clicked.
button1.interactable = false;
}
That would be put in a separate script. You would then replace the Button component on each of the buttons with this new script. Set all the values back to what they were and that’s it, no need to do anything else. Its using inheritance to get the full functionality of the Button component and adding that little bit in an overridden method that is called when the button is clicked automagically.
I mean you need to attach the script to the button in the scene. Like, drag the script onto the actual button’s game object in the scene list. The inspector should show the list of components, one of them would be “Button” (the Unity UI component) and one would be “ButtonScript” (the script you wrote). They need to be on the SAME object, on the actual button object. It sounds like you’ve got ButtonScript attached to some other different object.
If you’re just trying to do something like “When I click a button, this thing happens”, you shouldn’t need to use the EventSystem class or anything like that in your code at all. It sounds like you’re overcomplicating this. Some of the other methods people are mentioning (implementing a new button subclass, overriding the OnClick method, maintaining a list of button references, etc.) are all things you could do if you need complex behaviors or for some reason you aren’t able to attach scripts to the button, but for what you’re doing, you don’t need any of that. Just attach the script you want onto the button.
If all you want to do is make the button non-interactable you don’t even need a script, you could just do that in the inspector. Go to the OnClick section of the button, click the Plus sign to add a handler, drag the button itself into the blank, and then choose “Button → bool interactable” from the dropdown list, and uncheck the checkbox, to indicate that you want to set it to false when the button is clicked. You can put multiple handlers in the OnClick section if you want the button to do multiple things.