Field on the Inspector works but not on the GUI

In the above script I wanted pick-up apples with my player with OnCollisionEnter. This part works fine. Nevertheless, I found a problem when I try to pass the picked-up apples to the GUI interface because the collected apples do not register the won points on GUI. That is, the GUI line: carriedGui.text = "Carrying: " + carrying + " of " + carryLimit; shows on the screen without progress of the variable carrying (the picked-up apples). But if I watch this script on the Inspector, the field Carrying DOES register the apples (points). Any help, please!

  var carrying : int;
  var carryLimit : int;
  var deposited : int;
  var winScore : int;
  var appleCollect : AudioClip;
  var carriedGui : GUIText;      
  var depositedGui : GUIText;

  private var timeSinceLastPlay : float; 

  public function Start()
  {

    timeSinceLastPlay = Time.time;
    UpdateCarryingGui();
    UpdateDepositedGui();

  }

  function UpdateCarryingGui()
  {
    carriedGui.text = "Carrying: " + carrying + " of " + carryLimit; 
  }

  function UpdateDepositedGui()
  {
    depositedGui.text = "Deposited: " + deposited + " of " + winScore; 
  }

  function OnTriggerEnter(collisionInfo : Collider)
  {
    if(collisionInfo.gameObject.tag == "apple")
    {
      carrying++;
      audio.PlayOneShot(appleCollect);
      Destroy(collisionInfo.gameObject);
    }
  }

2 Answers

2

How about calling UpdateCarryingGui() when you increment the variable?

Do you mean putting carrying inside the function UpdateCarryingGui()? I did that but, it does not work. In fact, with this modification, on the Inspector appears one point on the start, which is worst than in the previous case.

What I missed was to call an update in the proper place (thanks Dreamora for your hint).

In other words, I added the line:

UpdateCarryingGui(); after the line:

carrying++; inside the function OnTriggerEnter

SOLVED