- Home /
Help With Adding Buttons To Panel
Hello! So I am trying to set up this code screen were whenever the user enters a working code it will add a button with that codes' name on it to the blue panel, and if they click on said button it will remove it from the panel. I need to be able to access each button that is entered into the panel by script to check which codes are activated. How would I go about doing this? Thank you for your help!
Answer by Epicnez · Mar 29, 2020 at 06:20 PM
@CobbledGames, @SwastikBhattacharyya, Wow! Thank you all for the detailed responses! I will get working trying them out and let you know how it works out! Thank you again!
Answer by SwastikBhattacharyya · Mar 29, 2020 at 01:19 PM
Steps of Solution:
@Epicnez perform these steps to do the required
1. Add a CheatCodeManager script to the Scene
Create a CheatCodeManager.cs script somewhere in your assets folder. Then, create an Empty Game Object in the Scene and add the script to that scene.
2. Add an Update() Function to the script to check the input in the Input Field
Store the codes in a string array. Say, codes.
public string[] codes = new string[$NUMBER OF CODES$] {$Codes$};
Add a public Button in the script and reference it to button in your scene.
public Button button;
Add a public InputField in the script and reference it to the InputField in your scene
public InputField inputField;
Disable the button in the start of your scene
public void Start()
{
button.gameObject.SetActive(false);
}
Set up a function that will check if the entered codes match any of the codes in the array.
public void CheckInput()
{
for (int i = 0; i < codes.Length; i++)
{
if (inputField.text == codes[i])
{
button.gameObject.SetActive(true);
}
}
}
Add this function to your update function.
So, after all this your script should look like this:
public class CheatCodeManager {
public Button button;
public InputField inputField;
public string[] codes = new string[1] {"Code"}; // For example
public void Start()
{
button.gameObject.SetActive(false);
}
public void Update()
{
CheckInput();
}
public void CheckInput()
{
for (int i = 0; i < codes.Length; i++)
{
if (inputField.text == codes[i])
{
button.gameObject.SetActive(true);
}
}
}
}
}
3. Remove the Button when clicked This step is simple, create a function called DisableButton inside the CheatCodeManager.cs script.
public void DisableButton()
{
button.gameObject.SetActive(false);
}
Now go to your inspector and open the Button component of your Button GameObject. Click the (+) sign on the OnClick () box then drag the GameObject containing the CheatCodeManager script. From the No Function Dropdown, choose your script and then the DisableButton() function. NOTE: The Function must be PUBLIC to be accessible in the dropdown
4. Perform the Task of the Entered Cheat Code
Add a Function called InitCheatEffect() and add an if function that will check the entered code and then perform the required result.
public void InitCheatEffect()
{
if (inputField.text == "code")
{
InitCodeEffect();
}
else if (inputField.text == "$SOMETHING ELSE$")
{
// Do Something Else
}
}
public void InitCodeEffect()
{
// Effect
}
Call this function in your DisableButton function created earlier. So after all this your CheatCodeManager script should look like this:
CheatCodeManager.cs
public class CheatCodeManager {
public Button button;
public InputField inputField;
public string[] codes = new string[1] {"Code"};
public void Start()
{
button.gameObject.SetActive(false);
}
public void CheckInput()
{
for (int i = 0; i < codes.Length; i++)
{
if (inputField.text == codes[i])
{
button.gameObject.SetActive(true);
}
}
}
public void DisableButton()
{
InitCheatEffect();
button.gameObject.SetActive(false);
}
public void InitCheatEffect()
{
if (inputField.text == "code")
{
InitCodeEffect();
}
else if (inputField.text == "$SOMETHING ELSE$")
{
// Do Something Else
}
}
public void InitCodeEffect()
{
// Effect
}
}
You can also store the value entered in the InputField in another public variable of the Class in the CheckInput fuction and then check the value of that variable in the InitCheatEffect() function.
Let me know if there are any problems and if this method worked for you. Thanks for reading!
Thank you for your response but it's not quite working the way I need it too. I think the best way to do this would be to start by doing a simpler version of it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CheatCode$$anonymous$$anagerVThree : $$anonymous$$onoBehaviour
{
public int onCode = 0;
public string[] activeCodes;
public InputField inputField;
public Text codeBox;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
inputField.text = inputField.text.ToUpper();
if (inputField.text == "SPEED")
{
activeCodes[onCode] = inputField.text;
Debug.Log("Speed Enabled!");
inputField.text = "";
onCode += 1;
}
if (inputField.text == "SLOW")
{
activeCodes[onCode] = inputField.text;
Debug.Log("Slow Enabled!");
inputField.text = "";
onCode += 1;
}
}
}
The goal of the above code is that the user would enter a code, if it registers, such as SPEED or SLOW it would add that code from the inputField.text to the activeCodes array so long as that code is not already in the array. Every frame that array will be set to the codeBox.text. I have the part where it checks if the input is a code and then adds it to the next index of the list, but I'm having trouble with the implementation of not being able to enter the same codes and having it added to the activeCodes array twice, and also setting textBox.text equal to the activeCodes array.
Once that is working perhaps then we can look into implementing buttons ins$$anonymous$$d of just text
Thank you again for your help, I really appreciate you taking the time to make a detailed response! You are a big help!
Answer by CobbledGames · Mar 29, 2020 at 01:22 PM
Essentially I can describe how it could work. First you need to check if the entered code matches one of your cheat codes.
If it is the same then a bool for that code being entered should be set to true. which should enable it's respective button object on the blue panel. Then if the player clicks on the enabled button, it should toggle a bool for that cheat being active as well as changing the buttons appearance to feedback to the player that this code is active. You would need to do this for each code.
Answer by Epicnez · Apr 17, 2020 at 09:22 PM
@CobbledGames @SwastikBhattacharyya Decided to use toggles in the panel, and then when someone enters a working code it will set a PlayerPref that will enable the corresponding toggle!