- Home /
GUI text Messages does not show and points counter do not stop at its prefixed limit
In my game the Player has to collect apples (like Penelope collects Orbs). Everything is working OK except that the the counter of apples (should stop in 5) that continues adding apples and the warning GUI message inside the function Pickup (else statement), which does not show the message "You can't carry any more apples". Thanks in advance for any help.
var carrying : int; var carryLimit : int; var deposited : int; var winScore : int; var appleCollect : AudioClip;
var carriedGui : GUIText;
var depositedGui : GUIText;
var guiMessage : GameObject;
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 Pickup()
{ if ( carrying < carryLimit ) { carrying++; UpdateCarryingGui();
}
else
{
var warning : GameObject = Instantiate( guiMessage );
warning.guiText.text = "You can't carry any more apples";
Destroy(warning, 2);
}
} function OnTriggerEnter(collisionInfo : Collider) { if(collisionInfo.gameObject.tag == "apple") { carrying++; UpdateCarryingGui(); audio.PlayOneShot(appleCollect); Destroy(collisionInfo.gameObject); } }
Answer by Ben Holmes · Apr 20, 2011 at 06:28 PM
you should be calling Pickup() in OnTriggerEnter() instead of just incrementing. As for your GUIText I would guess that either your camera doesn't have a GUILayer component or the position of your text is off the screen. GUIText transforms are in screen space so if you want it in the middle of the screen you put it at 0.5, 0.5.
The GUI layer and position are O$$anonymous$$. In fact, if I write a text in the corresponding field on the Inspector the text shows up. Regarding Pickup() inside OnTriggerEnter, I will tell you the results as soon I test it.
According to your recommendation (and the same advice from Jesse Anders) I placed the relevant code-lines from Pickup() to OnTriggerEnter() and the 2 problems were SOLVED. Thanks a lot!
Answer by burgunfaust · Apr 20, 2011 at 06:36 PM
The problem seems to be that there is no update to update the variables as you play through. You just set them and that's where they stay.
Put the following inside a function Update call:
function Update()
{
var carrying : int;
var deposited : int;
}
Since the limit of how many can be carried probably won't change, that can stay out. Same with win amount.
This does not resolved the problem. Thanks a lot, anyway.
You are destroying your warning gui too fast.It will pop up and then immediately be destroyed.
Your answer
![](https://koobas.hobune.stream/wayback/20220613124052im_/https://answers.unity.com/themes/thub/images/avi.jpg)