Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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 /
avatar image
0
Question by unity_EUg8wuaMBeWIww · Mar 16, 2018 at 06:09 PM · addnegative

Incrementing and decrementing a text value for a trade

Im attempting to add and minus from a text value using buttons. What my script currently does is - when I +++++ it shows 5, then I use the - button 3 times and it gives me -3, I then use + and I get 6. I hope this makes it clear.

public class PlusMinusScript : MonoBehaviour {

 private int textNumber;
 public Text TextObject = null;


 public void addOne(){
     if (TextObject != null) {
             textNumber++;
             TextObject.text = textNumber.ToString ();
     }
 }
 public void minusOne(){
     if (TextObject != null) {

             --textNumber;
             TextObject.text = textNumber.ToString();

     }
 }

}

Comment
Add comment · Show 9
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 Hellium · Mar 16, 2018 at 06:15 PM 1
Share

Are you sure you have only one script Plus$$anonymous$$inusScript to a gameobject ? You must not attach this script to your 2 buttons. Only one GameObject must hold this script, then, select your buttons, drag & drop the object holding this script and call the appropriate functions.

avatar image TreyH · Mar 16, 2018 at 06:16 PM 0
Share

Do you only use one instance of this component? If there are multiple instances (ie you have one for Plus and one for $$anonymous$$inus), then this will happen as they both have their own values for textNumber.

avatar image unity_EUg8wuaMBeWIww · Mar 16, 2018 at 06:43 PM 0
Share

@hellium Inside my plus $$anonymous$$us script I should put two buttons? @TreyH Same question to you :) Thanks for your speedy replies. Im hoping i can get this now

avatar image TreyH unity_EUg8wuaMBeWIww · Mar 16, 2018 at 06:44 PM 0
Share

Well not quite, we're asking if you've maybe got two "Plus$$anonymous$$inusScript" components sitting in your scene, with your two "Plus" and "$$anonymous$$inus" buttons each looking at unique ones.

avatar image unity_EUg8wuaMBeWIww unity_EUg8wuaMBeWIww · Mar 16, 2018 at 06:54 PM 0
Share

Ah, yes. Each button has a plus$$anonymous$$us script on it. This is where I am getting confused

avatar image TreyH unity_EUg8wuaMBeWIww · Mar 16, 2018 at 06:55 PM 0
Share

Then that is probably the issue. :-)

Remove one and have your buttons reference the same instance of that script.

edit: alternatively, you can just make the textNumber static if you only intend to have one number like this ever exist and be shared across other objects, but that might start you on bad habits with other situations.

avatar image unity_EUg8wuaMBeWIww · Mar 16, 2018 at 07:12 PM 0
Share

I, unfortunately, have 5 values that need to be incremented and decremented.

avatar image unity_EUg8wuaMBeWIww · Mar 16, 2018 at 07:15 PM 0
Share

YAYAYAYYY!!!! Thank you so much for your help and patience @TreyH

Show more comments

1 Reply

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

Answer by Hellium · Mar 16, 2018 at 07:09 PM

As expected, you have attached the PlusMinusScript script to two scripts. Since the value of textNumber is not shared between these two scripts, your problem occurs.

  1. Attach the PlusMinusScript to only one gameObject (to your TextObject for example)

  2. Drag & Drop the gameObject holding the Text component to the public field of the PlusMinusScript

  3. Select your first button, drag & drop the gameObject in step #1, and select the addOne function

  4. Select your second button, drag & drop the gameObject in step #1, and select the minusOne function

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

76 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

Related Questions

Need help with array script 1 Answer

Invisible Models in Unity??? 5 Answers

class array removeAt 1 Answer

.Add not a function of my array. 1 Answer

`Inventory' does not contain a definition for `Add' 2 Answers


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