Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Epicnez · Mar 28, 2020 at 10:54 PM · uibuttoninputfieldpanelstrings

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!alt text

screenshot-69.png (113.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Epicnez · Mar 29, 2020 at 07:11 PM 0
Share

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!

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

224 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why are my UI elements hidden behind the background? 3 Answers

Button Press Write Alphabet in textbox 0 Answers

What is the code to load a panel that is not a buildIndex scene? 1 Answer

Button onClick events will not be triggered 1 Answer

How do I send inputfield text to another script? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges