- Home /
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.
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.
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.
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
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");
}
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.
Your answer
Follow this Question
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