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;
	}
		

}

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.

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 :slight_smile:

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;
    }
}

@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);
	}
}