How can I set a word to be the "discount" word?
I am creating a "Shopping Cart" styled game and I would like to add a specific word that would enable the user to get a 10% discount. How can I set a certain word to enable the user to input & receive a discount?
I also would like the Discount button to be disabled after it is used once. How can I disable the button?
My code for the cart is as follows:
using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEditor;
public class CartManager : MonoBehaviour {
public InputField firstNameinput, lastNameinput;
private string firstName,lastName;
public Text welcomeMessage;
public Text totalAmountTextField, overallAmountTextField;
public float totalAmount, overallAmount; //total is subtotal without tax, overall is with tax
public InputField discountCode;
public int discountPercentage;
public GameObject contentPanel;
// Use this for initialization
void Start () {
createItem ("yoga mat","thick rubber mat",75, "yogamat");
createItem ("pants","orange leggings",25, "IMG_3081");
createItem ("shirt","pink",15, "resize");
createItem ("hat","fitted hat",10, "namaste");
createItem ("shoes","training shoe",60, "shoes");
createItem ("bracelet","one size",3, "IMG_2731 copy");
}
public void createItem(string itemName, string itemDesc, int itemCost, string itemImage) {
Debug.Log ("Creating an Item");
GameObject go = Instantiate (Resources.Load ("Item Panel") as GameObject);
go.transform.SetParent (contentPanel.transform, false);
item item = go.GetComponent<item> ();
item.itemName.text = itemName;
item.itemDesc.text = itemDesc;
item.itemCost.text = "$" + itemCost;
item.realCost = itemCost;
Sprite itemImage_temp = Resources.Load<Sprite> (itemImage);
item.itemImage.sprite = itemImage_temp;
}
// Update is called once per frame
void Update () {
}
public void updateFirstName() {
firstName = firstNameinput.text;
Debug.Log (firstNameinput);
updateDisplayText ();
}
public void updateLastName() {
lastName = lastNameinput.text;
Debug.Log (lastNameinput);
updateDisplayText ();
}
void updateDisplayText () {
welcomeMessage.text = "Welcome " + firstName + " "+ lastName;
}
public void applyDiscount () {
if (discountCode.text != "discount") {
totalAmount = totalAmount * (100 - discountPercentage) / 100;
Debug.Log (totalAmount);
totalAmountTextField.text = "$" + totalAmount;
}
}
}
Answer by sloankelly · Dec 30, 2016 at 03:29 PM
Shouldn't line 60 be:
discountCode.text == "discount"
Because you want to apply discount if the discountCode.text is the discount word.
Is the applyDiscount() method the onClick event for the button? If so, add a reference to the button in your script:
public Button applyDiscountButton;
If (discountCode.text == "discount") is true, then inside the if-block add:
applyDiscountButton.interactable = false;
That will prevent the player from clicking the button over and over again after applying the correct discount.
As for the actual text that is currently hard-coded to "discount" you could pull that in from a resource file or a database at run-time and put it in a private member field. Then change line 60 (again!) to:
discountCode.text == discountCodeWord;
@sloankelly This was extremely helpful! Im having issues with the part where it only allows the user to click the discount once still however. what do I input to make this statement work [If (discountCode.text == "discount") is true, then inside the if-block add:
applyDiscountButton.interactable = false;]
Add the Button field as I mentioned above and save the script. Go to the Unity editor and in the scene's hierarchy select the object that this script is on. Then, find the apply discount button in the project hierarchy and drag that into the 'applyDiscountButton' slot in the script inspector.
You should be good to go after that.
So I should add a separate script for the button?
If the applyDiscount() is the button click handler, then no. You already have a script that handles the click event. The Cart$$anonymous$$anager doesn't have a reference to the button though. It gets called by the button but it doesn't know about the button. $$anonymous$$y change would fix that. It's just like you have references to Text and InputField elements in the Cart$$anonymous$$anager script.
What is the name of the button that you press to apply the discount?