- Home /
WaitForSeconds wont work for me?
i am very new to scripting and im making a world of warcraft style game. i wrote this javascript so that when my player touches a loot item a GUIText will say "Loot Collected" and then disappear. my problem is that the text appears but wont disappear. here is the code-
var collectionText : GUIText;
function Start ()
{
collectionText.enabled = false;
}
function OnTriggerEnter (col : Collider)
{
if(col.gameObject.tag == "Player")
{
collectionText.enabled = true;
yield WaitForSeconds(0.5);
collectionText.enabled = false;
}
}
Are you by any chance destroying your loot game object from any other script from Player or so?
Because it will no more enter to OnTriggerEnter function... Try setting a variable when loot touched, something like 'isLootTouched=true', and then call WaitforSeconds in 'update' because update will be called always
I'm more familiar with C# so apologies if its wrong in js. When using WaitForSeconds it needs to be run in a coroutine, so try scripting it like this have your waitforseconds in a IEnumerator and call it when the player collides.
var collectionText : GUIText;
function Start ()
{
collectionText.enabled = false;
}
function OnTriggerEnter (col : Collider)
{
if(col.gameObject.tag == "Player")
{
StartCoroutine( DisplayText())
}
}
IEnumerator DisplayText()
{
collectionText.enabled = true;
yield WaitForSeconds(TextDisplayTime);
collectionText.enabled = false;
}
You are right, but in JS you are not forced to. So this should not be the problem.
Have you tried putting a print statement or debug.log in to see if its calling? Also have you made sure your player is tagged and that the script has a collider that is set to trigger?
"i am very new to scripting and im making a world of warcraft style game"
This gives me a huge smile every time I see it :) :) :) Thanks to Unity :)
Answer by ramp · Jan 08, 2015 at 12:35 PM
Hello,
need to change Tag name of the gameObject,Because Player tag is Default GameObject Tag,its already registered.So this should be the problem.your code is working fine if you rename the Tag,like this.
col.gameObject.tag == "Players"
Thanks Ram
the question says that the text appears, so I doubt the if statement is the problem.
thanks it is working now! like i said im new to coding so this was probably a noob mistake. thanks for your help.
you should check that you aren't setting the object to active in another script in that case! it might cause problems in the future, and it sounds like if it wasn't entering this if then something must be setting it to active!
Well done @ramp, how wrong I was :D
@BLASTER$$anonymous$$ASTERR remember to !
@Scribe, I think they can not accept an answer until they get at least 10 points.
Your answer
Follow this Question
Related Questions
I need help fixing my inventory sorting script. 1 Answer
Pause not working correctly 1 Answer
Getting started: Inventory script 0 Answers
Gun script help 1 Answer
Issues with Inventory Code (Fixed!!) 0 Answers