Script Eating Disorder

There are two scripts one is on a instatiated prefab in which click
on an item from a list( in this case kinds of foods)
Each food item has a name and restore amount for the hunger slider in the vitals script.
I’m setting up a delegate system, but I’m getting a null reference when calling the event.

From the mousemanager
	public delegate void OnEatMessageReceived(int amount);
	public static event OnEatMessageReceived onCompleteEatAction;

public void OnMouseDown()
	{
		Debug.Log ("You Clicked!");
		Debug.Log (FoodIDText.text);
		Debug.Log (FoodNameText.text);
		Debug.Log (FoodPriceText.text);
		Debug.Log (RestoreAmountText.text);
		int coins=Convert.ToInt32(FoodPriceText.text);
		tavern.UpdateCoins (coins);
		restoreamount =Convert.ToInt32(RestoreAmountText.text);
		Wait (30f);
		Debug.Log("Waiting!");
		Wait (restoreamount);
		Debug.Log ("Eating!");
		if(onCompleteEatAction != null)
		{
			onCompleteEatAction (restoreamount);
		}
		else
		{
			Debug.Log ("Nope it's null");
		}



void Start()
	{
		MouseManagerTavernFood.onCompleteEatAction +=OnEatFood;
	}


public void OnEatFood(int amount)
	{
		Debug.Log ("Eat Food!");
		Debug.Log ("Before: " + HungerSlider.value);
		HungerSlider.value += amount;
		Debug.Log ("After: " + HungerSlider.value);
	}

What am I missing? This is my first foray into delegates.
Thanks!

  1. because it is actually null.

  2. In your Awake() set it to a specific method:

mousemanager.onCompleteEatAction += OnEatFood;

@Gilead7

Im disregarding any other code you may have, but this should be the correct setup?

MouseManagerTavernFood.cs

public delegate void OnEatMessageReceived(int amount);
public static event OnEatMessageReceived onCompleteEatAction;

public static void notifyEat(int amount) {

    if (onCompleteEatAction != null) {
       onCompleteEatAction(amount);
    }
}

YourOtherScript.cs

void Awake() {

    MouseManagerTavernFood.onCompleteEatAction += onFoodEaten;
}

public void onMouseDown() {

    MouseManagerTavernFood.notifyEat(restoreAmount);
}

private void onFoodEaten(int amount) {
    // Your code
}

Hello !

What error you have? What lane says ? which variable result in null reference?

I had to have both scripts attached to something in the scene before it would work.