- Home /
how to diactive a button for a while
i want to diactive a button for a while, like enter a name first and the load tu another scene, if we don't enter the name we can't load to another scene
Answer by fafase · Nov 09, 2018 at 07:53 AM
It appears you want the button to be inactive until some value is given in an input field. That's how I understood.
So first you need to listen to the InputField and based on this, you make the button enabled.
public class ButtonAction:MonoBehaviour
{
public InputField inputField;
public Button button;
void Start()
{
button.interactable = false;
inputField.onValueChanged.AddListener(CheckValue)
}
private void CheckValue(string str)
{
button.interactable = (str.Length > 0);
}
}
CheckValue listens to the changes in the input field and if the user enters one character, the button is interactable.
Answer by game4444 · Nov 08, 2018 at 07:19 AM
You have to take your button in your script then you can enable disable button script on button object. public Button btn; you have to include namespace using unityEngine.UI; then in your script, in start function you have to write. btn.enable = false; when user enters name and press enter then write btn.enable = true; You have to check when user have completed entering text.
sorry i don't get it can you explained even more detail? sorry i'm still new in this thing. @game4444
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonEnable : $$anonymous$$onoBehaviour {
public Button btn;
bool enteredText;
// Use this for initialization
void Start ()
{
btn.enabled = false;
enteredText = false;
}
// Update is called once per frame
void Update ()
{
// Here is your code for input text , check when user has completed entering text make entered text bool true
if (enteredText)
{
btn.enabled = true;
}
}
}
Your answer

Follow this Question
Related Questions
Please help with playing music for button click 2 Answers
Unity UI How to make Multiple Clickable Next / Back Buttons? 3 Answers
How to control an AudioSource with an AudioClip? 2 Answers
Draw with GL by script 0 Answers
problem when clicking on Button 0 Answers