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 /
avatar image
1
Question by Furcifer-bellator · Nov 14, 2016 at 07:36 AM · c#uibuttonbutton trigger events

why won't my add button script work?!? (help appreciated)

i'm making a script where i have a score that is displayed in a ui text item and 2 buttons. where if i click the plus button it substracts one and if i click the minus button it adds one. now i have made a script to do that. when i make the buttons display a test text to the debug system it works fine but when i want to add my calculation function it stops working. i can't seem to find out why! i'll post my script below any help is appreciated!

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class TotalAttributPoints : MonoBehaviour {
     public Button plusbutton;
     public Button minusbutton; 
     public int totalAtrributePoints;
     public Text totalAttributePointCounterText;
 
     void Awake() 
         {
         int totalAttributePointCounter = totalAtrributePoints;
         Debug.Log (totalAttributePointCounter);
         }
         
 
 
     void Start ()
     {
         totalAttributePointCounterText.text = "" + totalAtrributePoints;
 
         Button btn = plusbutton.GetComponent<Button>();
         btn.onClick.AddListener(plusevent);
 
         Button bton = minusbutton.GetComponent<Button>();
         bton.onClick.AddListener(testbutton);
     }
     void testbutton() {
         Debug.Log ("het werkt!");
     }
 
     void update ()
     {
         
     }
         
     private int plusevent(int totalAttributePoints)
     {
         return totalAtrributePoints - 1;
     }
         
 
 }
 

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

2 Replies

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

Answer by ThePersister · Nov 14, 2016 at 09:20 AM

UPDATE

Hey @Furcifer-bellator. I've updated the script and added a unity package for you.

To update the Text component, you Have to set textComponent.text = totalAttributePoints every time you change it it does not change automatically, there are ways to do that, but I'll just stick to your pattern.

Here's a little package containing an example scene and the script. https://www.dropbox.com/s/acnfmb5sf8sfapx/PlusMinusSystem.unitypackage?dl=0

Note that I changed the title to "TotalAttributePoints", instead of "TotalAttributPoints", it was bothering me, haha.

We now update the variable as well as the text every time we change it through our events. Note that I'm using clamp and a "Max" variable to prevent our value from going out of preferred bounds.

I hope that helps, best of luck!!!

If it did, please accept my answer, it would mean a lot to me :)

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class TotalAttributePoints : MonoBehaviour
 {
     public int totalAttributePointsCurrent = 0;
     public int totalAttributePointsMax = 3; // Example value
     public Button plusbutton;
     public Button minusbutton;
     public Text totalAttributePointCounterText;
 
     void Start()
     {
         Debug.Log( "Total attribute points at start: " + totalAttributePointsCurrent );
         updateText();
 
         // You don't have to use GetComponent<Button>(), because these variables are already buttons.
         plusbutton.onClick.AddListener( plusEvent );
         minusbutton.onClick.AddListener( minusEvent );
     }
 
     // plus one.
     private void plusEvent()
     {
         changeAttributePoints(1);
     }
 
     // minus one.
     private void minusEvent()
     {
         changeAttributePoints(-1);
     }
 
     // changes attributePoints and calls update text.
     // also makes sure that attributePoints doesn't become negative nor exceed the Max value.
     private void changeAttributePoints(int difference)
     {
         totalAttributePointsCurrent = Mathf.Clamp(totalAttributePointsCurrent + difference, 0, totalAttributePointsMax);
         updateText();
     }
 
     // updates the attributeText display.
     private void updateText()
     {
         totalAttributePointCounterText.text = "Current Points: " + totalAttributePointsCurrent;
     }
 }

Comment
Add comment · Show 2 · 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 ThePersister · Nov 14, 2016 at 09:22 AM 1
Share

Warning, renamed the "totalAtrributePoints" variable to "totalAttributePoints", so that might reset the int to 0 if you copy it, if so, just set it again in the inspector!

avatar image Furcifer-bellator · Nov 14, 2016 at 01:49 PM 1
Share

it worked perfectly! thankyou i'll accept this as the answer to the question!

avatar image
1

Answer by Furcifer-bellator · Nov 14, 2016 at 12:15 PM

@ThePersister

Hey man! thanks for the reply

you helped me out a bunch allready but i can't get the new totalAttributepoints variable to show up in the text after substracting one or adding one. i thought this was possible because a variable changed in a script is taken above one added in the edite via a public function. i tried to circumvent this by making a new variable called total. the buttons do work now! when i press the plus button it keeps substracting and when i use the minus button it keeps adding just like i wanted! I hope you can help me out a little bit more thanks a bunch allready tho!

here is the script i have got now: i have added an if function that only adds if the total value is below the totalattribute value and one that only substracts if the value is above 0 points. some how the script keeps calling the total value 0 i can't figure out why and i can't figure out why the script won't update the new totalattribute text.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class TotalAttributPoints : MonoBehaviour {
     public Button plusbutton;
     public Button minusbutton; 
     public int totalAttributePoints;
     public Text totalAttributePointCounterText;
     private int newCount;
     private int Total;
 
     void Start ()
     {
         Total = totalAttributePoints += newCount;
         Debug.Log (Total);
 
         totalAttributePointCounterText.text = "" + Total;
 
         plusbutton.onClick.AddListener( plusEvent );
         minusbutton.onClick.AddListener( minusEvent );
     }
 
     void testbutton() {
         Debug.Log ("het werkt!");
     }
         
     private void plusEvent()
     {
         if ( Total > 0) 
         {
             newCount -= 1;
         }
 
         Debug.Log ("" + newCount);
         Debug.Log ("" + Total);
     }
         
     private void minusEvent()
     {
         if ( Total < totalAttributePoints) 
         {
             newCount += 1;
         }
 
         Debug.Log ("" + newCount);
         Debug.Log ("" + Total);
     }
 }
 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Cannot change Button interactable in a UnityAction 3 Answers

Changing anchor positions on UI? 0 Answers

How to keep your button selected after clicking away ??? 1 Answer

UI Button color 1 Answer

Button On Click event firing GetMouseButton within script 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