Unity InputField Text disappears when button is clicked - c#

In my Unity project I have a InputField that a user can type into and click a button to trigger an action preformed based on what they entered. When I type into the InputField and then press the button, the text that was entered empties before I am able to grab it using inputtedFieldName.text
I have a InputField with the button as a child. The InputField has a component that attaches the script. Attached to the button is a method is called when the button is pressed. It appears when the button is pressed the text that was in the inputField disappears.
What can I do to fix this?
Method the button is calling
public void EnterTextToConsole() {
if (textInputted.text == "" ) {
ConsoleLog.addToConsole("NOTHING??");
} else {
ConsoleLog.addToConsole(textInputted.text);
}

Related

How to assign button values ​to an event

I have a MainButton object with a Button component. A component has a property (or what it is) On Click (). This property can have an object and a method that will be executed by pressing a button. I tried to set these values ​​in the inspector, but these values ​​are not saved in the prefab, because they are assigned from Assets, and not from Scene. How to assign this method and object through programming? Who didn't understood - I need to change properties (object, method) of the event "OnClick()" by the script.
You're looking for the Unity.OnClick UnityEvent.
public class Example : MonoBehaviour
{
//Make sure to attach these Buttons in the Inspector
public Button m_YourFirstButton, m_YourSecondButton, m_YourThirdButton;
void Start()
{
//Calls the TaskOnClick/TaskWithParameters/ButtonClicked method when you click the Button
m_YourFirstButton.onClick.AddListener(TaskOnClick);
m_YourSecondButton.onClick.AddListener(delegate {TaskWithParameters("Hello"); });
m_YourThirdButton.onClick.AddListener(() => ButtonClicked(42));
m_YourThirdButton.onClick.AddListener(TaskOnClick);
}
void TaskOnClick()
{
//Output this to console when Button1 or Button3 is clicked
Debug.Log("You have clicked the button!");
}
void TaskWithParameters(string message)
{
//Output this to console when the Button2 is clicked
Debug.Log(message);
}
void ButtonClicked(int buttonNo)
{
//Output this to console when the Button3 is clicked
Debug.Log("Button clicked = " + buttonNo);
}
}

Change button sprites in scrollview in Unity 5.2

I have a scroll view with lots of buttons on it. when i press one button, that button needs to change the sprite and stay like that. if any other button (or same one) is pressed previous button needs to revert back to original sprite..
here is some example
button 2 was pressed and changed sprite, it stays like that untill it is pressed again or any other (in this case button 3) is pressed
Here's a draft of a possible solution. Create a manager script for the "buttons group" and attach the script on the scrollview
public class ButtonsGroupController : MonoBehaviour
{
// List of all children buttons
private readonly List<Button> _buttons;
// Last pressed button index
private int _lastId = -1;
void Start(){
// Look for all buttons (children of this GO - scrollview)
_buttons = GetComponentsInChildren<Button>();
// Add event listener for buttons
for(var i = 0; i < _buttons.Count; i++){
var index = i;
_buttons[index].onClick.AddListener(() => ButtonPressed(index));
}
}
// Resolve buttons press event
private void ButtonPressed(int index){
// The same button has been pressed
if(_lastId == index)
return;
// Update the last selected button sprite - to normal state
// _buttons[_lastId]....
// Update the last id
_lastId = index;
// Update the sprite of newly pressed button
// _buttons[_lastId]....
}
}

reset the color of a previously pressed button after another button is pressed c#

I'm using C# windows form application, and i'm facing a problem resetting a button's back Color.
By clicking a button I need it to change its back color and reset the back color of previously pressed button.
Please note that i have lots of buttons in the form and i'm using "sender" to apply same clicking event to all buttons.
You'll need to keep track of the last button that was clicked. Add a private field and then manipulate it in the on click event handler:
class Form1 : Form
{
private Button _lastButtonClicked;
protected void ClickHandler(object sender, EventArgs e)
{
if (_lastButtonClicked != null)
_lastButtonClicked.BackColor = Color.whatever;
_lastButtonClicked = sender as Button;
_lastButtonClicked = Color.newcolor;
}
}

How to trigger button click event on pressing enter button in c#

I am creating a login form in windows phone app , I want to trigger login button event on pressing enter button from finishing the form.
Requirement: While clicking enter button from last text field in login page should trigger login button event..
Code sample
I know how to move from one text field to another on clicking enter button as below
private void passkeydown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
nextfield .Focus();
}
}
But I don't know how to trigger button click event here.
Try This:
if (e.Key == Key.Enter)
{
LoginButton_Click(sender,e); //here LoginButton_Click is click eventhandler
}

TaskBarButton middle mouse button event

I have a TaskBar with buttons. at the TaskBar there is a lot of events, but there is only one event at the click of a button.
TaskBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.TaskBarButtonClick);
I need an event to a button press TaskBar middle mouse button.
something like
if (e.Button == MouseButtons.Middle)
{
MessageBox.Show("Middle");
}
only taskbar
I know this example. I did that. the problem is that the event for the Taskbar. I need an event to the button provided on this TaskBar
e.Button is not of type MouseButtons. It is of type ToolBarButton. So it references the location on the toolbar that is clicked, not the location on the mouse used to make the click.
Toolbar Button
If you need to handle which toolbar button is clicked then reference this example for using the ToolBarButtonClickEventHandler works.
//add some buttons.
TaskBar.Buttons.Add(new ToolBarButton()); //index 0
TaskBar.Buttons.Add(new ToolBarButton()); //index 1
//add the handler
TaskBar.ButtonClick += new ToolBarButtonClickEventHandler (
this.taskbar_ButtonClick);
private void taskbar_ButtonClick (Object sender, ToolBarButtonClickEventArgs e)
{
// Evaluate the Button property to determine which button was clicked.
switch(TaskBar.Buttons.IndexOf(e.Button))
{
case 0:
//Whatever you want to do when the 1st toolbar button is clicked
break;
case 1:
//Whatever you want to do when the 2nd toolbar button is clicked
break;
}
}
Mouse Button
You could add an event handler for the MouseDown event to trap the Mouse button that was clicked.
TaskBar.MouseDown += new MouseEventHandler(this.taskbar_MouseDown);
private void taskbar_MouseDown(object sender, MouseEventArgs e)
{
// Determine which mouse button is clicked.
if(e.Button == MouseButtons.Middle)
{
MessageBox.Show("Middle");
}
}

Categories