Tell me, please, can I get the name of a UI-button in Unity from the method which is hung on my button (OnClick())? How?
Add Event Trigger in Inspector to your Button
click Add New Event Type and add a pointer click event.
drag your gameobject containing this script in the script area.
assign the below function in the function area.
assign the button in the parameter area:
public void OnClicked(Button button)
{
print(button.name);
}
Related
I'm fairly new to this.
When I click on the button to rebind the jump key, the rebind works fine. First the panel "Waiting on rebinding..." pops up, then I press the key, I want jump to be, then the new key shows up on the button as the binding.
This works only once though. When I want to click on the same button again, it doesn't allow me to click on it. The button doesn't even change color anymore, when you hover over it.
What I tried to solve this was to duplicate the button. When I click on either of the buttons and rebind the jump key, it works the first time, but not after that. Both buttons are unclickable. Closing and opening the button panel doesn't work either.
Here is the code for the OnClick() on the button:
public void JumpStartRebinding()
{
startRebindObject.SetActive(false);
waitingForInputObject.SetActive(true);
movement.PlayerInput.SwitchCurrentActionMap("UI");
jumpAction.action.Disable();
rebindingOperation = jumpAction.action.PerformInteractiveRebinding()
.WithControlsExcluding("Mouse")
//.WithCancelingThrough("<Keyboard>/escape")
.OnMatchWaitForAnother(0.1f)
.OnComplete(operation => JumpRebindComplete())
.Start();
}
private void JumpRebindComplete()
{
int bindingIndex = jumpAction.action.GetBindingIndexForControl(jumpAction.action.controls[0]);
bindingDisplayNameText.text = InputControlPath.ToHumanReadableString(
jumpAction.action.bindings[bindingIndex].effectivePath,
InputControlPath.HumanReadableStringOptions.OmitDevice);
jumpAction.action.Enable();
rebindingOperation.Dispose();
startRebindObject.SetActive(true);
waitingForInputObject.SetActive(false);
movement.PlayerInput.SwitchCurrentActionMap("Player");
}
The issue doesn't appear to be with your code. I was having this exact same issue a few days ago. It likely has to do with the fact that a UI object is "covering up" the button. A first step I'd take is making sure that nothing is obscuring your button after re-enabling it (often times, TextMeshProUGUI objects with "Raycast Target" enabled are the culprit).
enter image description here
Unity has a component called Button as part of its UI system which you can use to subscribe on-click events to it through the inspector which is incredibly useful.
However, when projects get larger, I run into trouble in many situations:
events subscribed this way in the inspector are not rearrange-able which makes buttons that have lots of events difficult to manage
changing the contents of the scripts used for events can cause the button to not recognize a function that was used for an event which means you have to re-reference it
if anything happens to the GameObject or prefab that stores the Button component such as it getting corrupt then all your events that were serialized onto the button would be wiped and you would need to re-reference all of them
the above points make debugging very very difficult
What are some ways I can work around the problems I've listed above?
Inject event functions inside the code:
public Button playButton; // set button in inspector
public void Start()
{
playButton.onClick.AddListener(() =>
{
transform.position = point1;
// do something..
});
}
So I have a pause menu created in unity. There is a panel and on it, there are three buttons but they do not seem to be working, meaning I can not click on them. Also, interactable of every button is checked and event system is present in the hierarchy.
Hierarchy image
Following is the code for the resume button:
public void resumegame()
{
levemusic.UnPause();
pausemenu.SetActive(false);
Cursor.visible = false;
gameisPaused = false;
Time.timeScale = 1;
}
Either disable this component on the panel
Or move the panel object such that it is on top of the hierarchy in the object hierarchy
I want to access button properties such as .interactable = false; of a button which is inside a GameObject. I want to access the button via GameObject. I tried below method but i could only access few properties like .SetActive.
gameObjectA.transform.GetChild(0).gameObject.SetActive(false);
where gameObjectA is my GameObject and GetChild(0) is my Button
I want to access other properties such as .Color, .Interactable etc.
Please Help. Thanks in Advance
Try using GetComponentInChildren, this will return the first instance of a button found in the gameobject or one of its children (recursive)
Button childbutton = GetComponentInChildren<Button>();
childbutton.interactable = false;
If you have several children with buttons you can the gameObject.transform.GetChild() that you were already using with a normal GetComponent(replace x with the child index)
Button childbutton = gameObjectA.transform.GetChild(x).GetComponent<Button>();
You can also assign and read tags or names if you are changing and adding buttons constanly and dont want to rechange your code
How to get Gameobject automatically with script?
I need know. button OnClick method empty if the scene changes. My gameObject tpye Don'tDestroyLoad.
Instead of changing the object in the field you can use a script to then have GameObject gameObjectToClick to be changed. Use the inspector to add a script to the button called "Click", then drag the button object into the object field shown in the screenshot and then select the function OnClick(). Click has the following code:
using UnityEngine;
using System.Collections;
public class Click : MonoBehaviour {
public GameObject gameObjectToClick;
void OnClick(){
gameObjectToClick.SpecialFunction(); //call special function you wanted to call
}
}
Simply change gameObjectToClick through another script how you would normally achieve it and change it (e.g button.GetComponent<Click>().gameObjectToClick = otherGameObject) Also change SpecialFunction() to the function you would have set in the dropdown menu in the screenshot.