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
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*)*
{
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*)*
{
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*)*
{
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!
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.
@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!
@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!