UI question, 2 buttons and 1 textfield doesnt interact

I have two UI buttons. One buy button and one sell button. When I press the buy button the value of what increases by 1 and when I press the sell button it decreases by one. The problem is that the value displayed in the wheatDisplay.text is only modified by pressing the buy button.

 using UnityEngine;
 using System.Collections;
    
    public class Buy : MonoBehaviour 
    {
    
    public UnityEngine.UI.Text wheatDisplay;
    public int wheat = 0;
    
    
         void Update() 
         {
              wheatDisplay.text = "Wheat: " + wheat;
         }
    
         public void bought()
         {
               wheat = wheat + 1;
        }
    
    }

using UnityEngine;
using System.Collections;

public class Sell : MonoBehaviour {

	public UnityEngine.UI.Text wheatDisplay;
	public int wheat = 0;
	
	// Update is called once per frame
	void Update () {
		wheatDisplay.text = "Wheat: " + wheat;
	}

	public void sold(){
		wheat = wheat - 1;
	}

}

Your “wheat” variable lies at class scope, and these are two different classes. That means two unique “wheat” variables which are entirely unrelated.

You should consolidate this code into some kind of system. I assume you may want more buttons and more types of items in the future. You should not design your program in such a way that you’re forced to write redundant code, (i.e. a new script for every item or button.)

Have a PlayerAccount which has a list of InventoryItems, and hook your buy/sell buttons to a Vendor script which can examine InventoryItems in your possession and respond to buy/sell clicks appropriately. The button can tell the vendor script which button it is, so you won’t need a script for every button.

Take some time to get familiar with the language before you get too entrenched in any one thing. I know it’s a pain, but you’ll be glad you did. Dig into some tutorials and lessons on the site, and maybe check out this C# guide:

http://rbwhitaker.wikidot.com/c-sharp-tutorials