Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 /
  • Help Room /
avatar image
0
Question by Damanig · Dec 29, 2016 at 04:04 AM · unity 2dbuttonssetactivedisable object

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;

     }

 }

}

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

1 Reply

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

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;
Comment
Add comment · Show 22 · 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 Damanig · Dec 30, 2016 at 10:01 PM 0
Share

@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;]

avatar image sloankelly · Dec 30, 2016 at 10:30 PM 0
Share

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.

avatar image Damanig sloankelly · Dec 30, 2016 at 10:33 PM 0
Share

So I should add a separate script for the button?

avatar image sloankelly Damanig · Dec 30, 2016 at 10:36 PM 0
Share

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.

Show more comments
Show more comments
avatar image sloankelly sloankelly · Dec 31, 2016 at 02:40 AM 0
Share

What is the name of the button that you press to apply the discount?

avatar image Damanig sloankelly · Dec 31, 2016 at 02:43 AM 0
Share

The button reads "Discount (10%)"

Show more comments
Show more comments

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

84 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

Related Questions

Rotating around z-axis with OnScreen buttons 1 Answer

SetActive the first button that is not active in order of an array 0 Answers

How to make UI scale equally to camera? 1 Answer

How do I switch from * Input.GetAxis () * to UI buttons? 0 Answers

Input.Touches Mouse position Shooting Buttons 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