How to fix button clones that aren't working?
I have buttons that are made on startup and given a onclick listener etc, but when you try to click them in game they are completely ignored as if they aren't even there and it just hits the panel behind them, the button to open the store works perfectly, I did it so they change colour when pressed and highlighted and only the store button changed. I will post the code for the classes if it has something to do with them.
This is my first class (ShopItemHandler)
public GameObject button;
int counter;
public ItemScript ItemScript;
[System.Serializable]
public class Item {
public string objectName;
public float cost;
public float gain;
public int count;
}
public GameObject[] buttonItems;
public Item[] shopItems;
// Use this for initialization
void Start () {
buttonItems = new GameObject[shopItems.Length];
counter = 0;
foreach (Item i in shopItems) {
GameObject btn = (GameObject)Instantiate (button);
buttonItems [counter] = btn;
ItemScript scp = btn.GetComponent<ItemScript> ();
scp.objectName.text = i.objectName;
scp.cost.text = "Cost: " + i.cost.ToString("F1");
scp.count.text = i.count.ToString();
scp.gain.text = "Eggs/S: " + i.gain.ToString("F1");
btn.transform.SetParent(transform, false);
Item thisItem = i;
scp.thisButton.onClick.AddListener (() => Purchase(thisItem));
counter++;
}
}
public void Purchase (Item Bought) {
Bought.count++;
Bought.cost = Bought.cost * 1.2f;
updateItems ();
}
void updateItems () {
counter = 0;
foreach (Item i in shopItems) {
ItemScript scp = buttonItems[counter].GetComponent<ItemScript> ();
scp.objectName.text = i.objectName;
scp.cost.text = "Cost: " + i.cost.ToString("F1");
scp.count.text = i.count.ToString();
scp.gain.text = "Eggs/S: " + i.gain.ToString("F1");
counter++;
}
}
// Update is called once per frame
void Update () {
}
And this is the class script for the buttons/Items (ItemScript)
public Text objectName;
public Text cost;
public Text gain;
public Text count;
public Button thisButton;
// Use this for initialization
void Start () {
thisButton.gameObject.SetActive (true);
}
// Update is called once per frame
void Update () {
}
Your answer
Follow this Question
Related Questions
Dymanic buttons with different openURL links 0 Answers
Button make object teleport (simple) 1 Answer
How to make an object keep move when pressing a button? 0 Answers
How to make button rotate when another button is pressed? 0 Answers
Wanting make shop system, how can I disable this condition after item sold? 0 Answers