I have a simple script, I want it to display “+15 bullets” when you pick up a clip, then wait 2 seconds then make the text dissapear, but for some reason the text just gets stuck, and wont disappear!
function LateUpdate(){
Start();
}
function Start (){
if (PickupIonClip.used == true)
{
guiText.enabled = true;
guiText.text = "+15 Bullets";
yield WaitForSeconds (2);
guiText.enabled = false;
} else{
guiText.enabled = false;
}
}
Ive tried using Update function instead of LateUpdate, but after 2 seconds, the text lags and flickers for some reason, maybe because Update runs every frame or something. I also cant pick up any more clips after picking up 1 clip… Ive put this script onto the GUIText, so it should work, this is really stumping me :X
Whats wrong with my script?
EDIT: Heres the pick up script just in case.
//enum PickupType { Health = 0, Rocket = 1, Clips = 2 }
static var pickupType = PickupType.Clips;
var amount = 0;
var sound : AudioClip;
static var used = false;
function ApplyPickup (player : FPSPlayer) {
if (pickupType == PickupType.Clips) {
if (IonPistol.clipsLeft < 90) {//maximum capacity of clips is 90
IonPistol.clipsLeft = IonPistol.clipsLeft + amount;
return true;
}
else if (IonPistol.clipsLeft >= 90){
return false;
}
}
return true;
}
function OnTriggerEnter (col : Collider) {
var player : FPSPlayer = col.GetComponent(FPSPlayer);
//* Make sure we are running into a player
//* prevent picking up the trigger twice, because destruction
// might be delayed until the animation has finnished
if (used || player == null)
return;
if (!ApplyPickup (player))
return;
used = true;
// Play sound
if (sound)
AudioSource.PlayClipAtPoint(sound, transform.position);
// If there is an animation attached.
// Play it.
if (animation animation.clip) {
animation.Play();
Destroy(gameObject, animation.clip.length);
} else {
Destroy(gameObject);
}
}
// Auto setup the pickup
function Reset () {
if (collider == null)
gameObject.AddComponent(BoxCollider);
collider.isTrigger = true;
}
Bump? Anyone already have this script made?
i only have experience in c# but i’ll try to help.
from the code you provided, seems like the variable “used” was not set back to false, so it only works for the first time, because of
if (used || player == null)
return;
as for your main problem, i can’t help. but you could try:
function LateUpdate(){
Start();
}
function Start (){
if (PickupIonClip.used == true)
{
guiText.enabled = true;
guiText.text = "+15 Bullets";
yield WaitForSeconds (2);
guiText.enabled = false;
print("disabled after 2 seconds");
} else{
guiText.enabled = false;
print("disabled (used == false)");
}
}
to have an idea on what’s going on. that’s what i would do, because i am not sure whether the disabling part of the code is actually triggered.
good luck
wait.
LateUpdate is called every frame.
your variable “used” is set to true on pickup.
and “used” is not reset to false.
so this if-statement:
if (PickupIonClip.used == true)
will always be true, once you have picked up a clip.
and therefore the text is being enabled all the time (and pick up only works once)
correct?
Make sense… So how would I set that to false after a couple of seconds?
Alright well it works, but while the text is showing, the clip on the floor still shows, until after 2 seconds, the text dissapears, then the clip dissapears, how would i fix that? because the player is able to pick up the clip multiple times walking past it, its not very logical…
function LateUpdate(){
Start();
}
function Start (){
if (PickupIonClip.used == true)
{
guiText.enabled = true;
guiText.text = "+15 Bullets";
} else{
guiText.enabled = false;
}
}
//enum PickupType { Health = 0, Rocket = 1, IonClips = 2 }
var pickupType = PickupType.IonClips;
var amount = 0;
var sound : AudioClip;
static var used = false;
function ApplyPickup (player : FPSPlayer) {
//for the clips pickup ONLY
if (pickupType == PickupType.IonClips) {
if (IonPistol.clipsLeft < 90)//maximum capacity of clips is 90
IonPistol.clipsLeft = IonPistol.clipsLeft + amount;
else if (IonPistol.clipsLeft >= 90)
return false;
}
return true;
}
function OnTriggerEnter (col : Collider) {
var player : FPSPlayer = col.GetComponent(FPSPlayer);
//* Make sure we are running into a player
//* prevent picking up the trigger twice, because destruction
// might be delayed until the animation has finnished
// if (used || player == null)
// return;
if (!ApplyPickup (player))
return;
used = true;
yield WaitForSeconds (0.5);
used = false;
// Play sound
if (sound)
AudioSource.PlayClipAtPoint(sound, transform.position);
// If there is an animation attached.
// Play it.
if (animation animation.clip) {
animation.Play();
Destroy(gameObject, animation.clip.length);
} else {
Destroy(gameObject);
}
}
// Auto setup the pickup
function Reset () {
if (collider == null)
gameObject.AddComponent(BoxCollider);
collider.isTrigger = true;
}
after thinking about it, i think your previous implementation is somewhat correct. the clips SHOULD be usable only once.
initialize instances of the clips which are usable only once (during loading), then put them in a pool like this. then u can put them back into a pool after it has been “collected”.
the problem was the text. possibly you could make the “used” flag independent of the picking-up logic, either:
a) have 2 flags, one for displaying text and one for picking up
b) immediately set the clip object’s enabled flag to false on pickup
(b) is actually similar to (a), just that it uses an existing flag (enabled) on the clip object. tho i’m not 100% sure if the rest of the script will run if the object was disabled. but i think it will