variable returns from referenced script as 0

I try to reference a variable in another script and it comes back as 0, no idea why, here’s my code

void OnTriggerEnter2D(Collider2D other)
{
        if(other.gameObject.tag == "candy")
        {
            candyKainz++;
            other.gameObject.GetComponent<Candycaine>().collected();
        }
}

public class Counter : MonoBehaviour {
    public Text numeber;
    public Characterbehavior h;
   
    // Use this for initialization
    void Start () {
        
    }
	
	// Update is called once per frame
	void Update () {      
        numeber.text = h.candyKainz + "/100";      
	}   
}

you are not referencing a variable anywhere in the code you posted. you are referencing a Candycaine component with the line:

other.gameObject.GetComponent<Candycaine>().collected();

and calling a function (collected()) in the Candycaine component.

Your “Characterbehavior” needs to be assigned. If it extends a MonoBehaviour, you can add this through the inspector or calling a “GetComponent” on a GameObject which does have one. Other wise you use: h = new Characterbehavior() to create a new instance and assign candyKainz before you try to read the variable.