Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Darkwinger · Aug 27, 2016 at 06:53 AM · functionnamenamingnames

Using a number/brackets in a function name?

So basically I have a script which selects buttons, and a "buy" button, I'm converting the selected buttons name to a string and calling a function with it, when buy button is clicked. Though when I create a function name like 'Button (1) () 'it doesn't compile, any help is appreciated, thanks in advance!

P.S. Sorry I can't show my scripts but hopefully my description is clear enough.

Comment
Add comment · Show 4
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 NoseKills · Aug 27, 2016 at 08:28 AM 0
Share

Edit the button's name to be just "Button1". Though what you seem to be doing is kinda hacky i think. Are you using reflection ? If you used for example a Dictionary<string, Action>, you wouldn't have this problem.

avatar image Darkwinger · Aug 27, 2016 at 08:54 AM 0
Share

What I did was duplicate a bunch of buttons for a shop then have them parent a mesh for a character shop, and since Unity automatically gives button names such as Button (1) I tried to use them in a function, so I know which character I'm buying from the nearest button to the centre. If I couldn't use these strings could I change the names manually by script? Because I don't really want to go through all of them in the Hierarchy.And that answer was supposed to be a reply, I misclicked because I'm on a phone.

avatar image Darkwinger · Aug 27, 2016 at 05:51 PM 0
Share

Sorry, I'm a bit a a noob when it comes to some stuff, could you explain how that would work. Also the reason I have brackets is because if I duplicate another button for another item to buy, it'll automatically be numbered again with the same pattern like Duplicating Button (47) makes Button (48) in the hierarchy. @Nose$$anonymous$$ills

avatar image NoseKills Darkwinger · Aug 27, 2016 at 09:53 PM 0
Share

It's pretty hard to help without seeing any code, but whatever strings you need to map to function calls, you could do something like this

 private Dictionary<string, Action> buttonFunctions;
 
     private void Awake() {
         buttonFunctions = new Dictionary<string, Action>();
         buttonFunctions.Add("Button(1)", ButtonFunction1);
         buttonFunctions.Add("Button(2)", ButtonFunction2);
     }
 
     private void ButtonPressed(string buttonName) {
         Action buttonAction;
         if (buttonFunctions.TryGetValue(buttonName, out buttonAction)) {
             buttonAction();
         } else {
             Debug.LogError("Button " + buttonName + " was pressed but there was not Action for it in 'buttonFunctions'");
         }
     }
 
     private void ButtonFunction1() {
         Debug.Log("Button 1 was pressed");
     }
 
     private void ButtonFunction2() {
         Debug.Log("Button 2 was pressed");
     }

1 Reply

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

Answer by Darkwinger · Aug 28, 2016 at 03:41 PM

@NoseKills Your post really help guide me in the right direction, I posted the stages of my script here, incase anyone wanted to see it, I went from this mess:

 public Boss boss;
 private int SelectedCharacter;
 private GameObject highlighted;
 private string highlightedString;
 public GameObject[] charcs;
 public bool[] unlocked;
 public int cash;

 void Start(){
     
 }
 void Update(){
     highlighted = boss.minButtonNum;
     highlightedString = highlighted.name;
     cash = PlayerPrefs.GetInt ("cash", 0);
 }

 public void OnClick(){
     if (highlightedString == "Button (1)") {
         Button1 ();
     }
     if (highlightedString == "Button (2)") {
         Button2 ();
     }
     if (highlightedString == "Button (3)") {
         Button3 ();
     }
     if (highlightedString == "Button (4)") {
         Button4 ();
     }
     if (highlightedString == "Button (5)") {
         Button5 ();
     }
 }



 void Button1(){
     if (PlayerPrefs.GetInt ("Bought_" + highlightedString, 0) == 1) {
         SelectedCharacter = 1;
         PlayerPrefs.SetInt ("SelectedCharacter", SelectedCharacter);
         Debug.Log (SelectedCharacter);
     } else if (cash > 100) {
         SelectedCharacter = 1;
         PlayerPrefs.SetInt ("SelectedCharacter", SelectedCharacter);
         PlayerPrefs.SetInt ("Bought_" + highlightedString, 1);
         Debug.Log (SelectedCharacter);
         cash -= 100;
     }
 }
 void Button2(){
     SelectedCharacter = 2;
     PlayerPrefs.SetInt ("SelectedCharacter", SelectedCharacter);
     Debug.Log (SelectedCharacter);
 }
 void Button3(){
     SelectedCharacter = 3;
     PlayerPrefs.SetInt ("SelectedCharacter", SelectedCharacter);
     Debug.Log (SelectedCharacter);
 }
 void Button4(){
     SelectedCharacter = 4;
     PlayerPrefs.SetInt ("SelectedCharacter", SelectedCharacter);
     Debug.Log (SelectedCharacter);
 }
 void Button5(){
     SelectedCharacter = 5;
     PlayerPrefs.SetInt ("SelectedCharacter", SelectedCharacter);
     Debug.Log (SelectedCharacter);
 }

To this:

 void Awake() {
     buttonFunctions = new Dictionary<string, Action>();
     buttonFunctions.Add("Button (1)", ButtonFunction1);
     buttonFunctions.Add("Button (2)", ButtonFunction2);

 }

 void Update () {
     highlighted = boss.minButtonNum;
     highlightedString = highlighted.name;
     cash = PlayerPrefs.GetInt ("cash", 0);
 }
 public void OnClick(){

     Action buttonAction;
     if (buttonFunctions.TryGetValue(highlightedString, out buttonAction)) {
         buttonAction();
     } else {
         Debug.LogError("Button " + highlightedString + " was pressed but there was not Action for it in 'buttonFunctions'");
     }
 }

 void Buying(){
     if (PlayerPrefs.GetInt ("Bought_" + highlightedString, 0) == 1) {
         SelectedCharacter = int.Parse(highlightedString);
         PlayerPrefs.SetInt ("SelectedCharacter", SelectedCharacter);
         Debug.Log (SelectedCharacter);
     } else if (cash > 100) {
         SelectedCharacter = 1;
         PlayerPrefs.SetInt ("SelectedCharacter", SelectedCharacter);
         PlayerPrefs.SetInt ("Bought_" + highlightedString, 1);
         Debug.Log (SelectedCharacter);
         cash -= 100;
     }
 }



 private void ButtonFunction1() {
     Debug.Log("Button 1 was pressed");
 }

 private void ButtonFunction2() {
     Debug.Log("Button 2 was pressed");
 }
 }

And finally ended up using this system, which also allows me to have more objects in editor to buy without having to edit the scripts:

using System.Text.RegularExpressions;

public class Selector : MonoBehaviour {

 public Boss boss;
 private int SelectedCharacter;
 private int WantedSelectedCharacter;
 private GameObject highlighted;
 private string highlightedString;
 public int cash;

 void Awake() {
     //If i need to test>>PlayerPrefs.SetInt ("cash", 100);
 }

 void Update () {
     highlighted = boss.minButtonNum;//selectedbutton
     highlightedString = highlighted.name;
     cash = PlayerPrefs.GetInt ("cash", 0);
 }
 public void OnClick(){


     Buying ();
 }

 void Buying(){
     Debug.Log (cash);
     if (PlayerPrefs.GetInt ("Bought_" + highlightedString, 0) == 1) {
         Debug.Log ("Cash is still "+cash);
         WantedSelectedCharacter = int.Parse (Regex.Replace (highlightedString, "[^0-9]", ""));
         //^^Getting int from the name of selected Button^^
         SelectedCharacter = WantedSelectedCharacter;
         PlayerPrefs.SetInt ("SelectedCharacter", SelectedCharacter);
         Debug.Log ("Selected Character is" + SelectedCharacter);
     } else if (cash >= 100) {
         Debug.Log ("Cash is "+cash);
         WantedSelectedCharacter = int.Parse (Regex.Replace (highlightedString, "[^0-9]", ""));
         SelectedCharacter = WantedSelectedCharacter;
         PlayerPrefs.SetInt ("SelectedCharacter", SelectedCharacter);
         PlayerPrefs.SetInt ("Bought_" + highlightedString, 1);
         Debug.Log ("Selected Character is" + SelectedCharacter);
         cash -= 100;
         PlayerPrefs.SetInt ("cash", cash);
     } else {
         Debug.Log ("Not enough money to buy character" + highlightedString);
     }
 }
     

}

You really help guide me in the right direction,thanks 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

how return array name in array 2 Answers

Match name to variable name? 1 Answer

OnTriggerEnter just if name of trigger is something 2 Answers

GetName can only be called from the main thread. 2 Answers

Get name of Scene-file from MonoBehaviour 1 Answer


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