Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by BLASTERMASTERR · Jan 08, 2015 at 08:25 AM · javascriptinventorywaitforsecondstimescale

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;
    }
 }

Comment
Add comment · Show 10
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HarshadK · Jan 08, 2015 at 08:57 AM 0
Share

Are you by any chance destroying your loot game object from any other script from Player or so?

avatar image Pragnani K · Jan 08, 2015 at 09:16 AM 0
Share

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

avatar image stevecus · Jan 08, 2015 at 09:55 AM 1
Share

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;
 }
avatar image KiraSensei stevecus · Jan 08, 2015 at 09:55 AM 0
Share

You are right, but in JS you are not forced to. So this should not be the problem.

avatar image Priyanshu stevecus · Jan 08, 2015 at 09:59 AM 0
Share

His code in JS is same as yours in C#.

avatar image stevecus stevecus · Jan 08, 2015 at 10:53 AM 0
Share

Ah ok apologies

Show more comments
avatar image stevecus · Jan 08, 2015 at 10:56 AM 0
Share

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?

avatar image Xtro · Jan 08, 2015 at 06:52 PM 0
Share

"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 :)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
5

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Scribe · Jan 08, 2015 at 12:40 PM 0
Share

the question says that the text appears, so I doubt the if statement is the problem.

avatar image BLASTERMASTERR · Jan 08, 2015 at 05:47 PM 0
Share

thanks it is working now! like i said im new to coding so this was probably a noob mistake. thanks for your help.

avatar image Scribe · Jan 08, 2015 at 05:49 PM 2
Share

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 alt text!

avatar image Paulius-Liekis · Jan 08, 2015 at 10:38 PM 0
Share

@Scribe, I think they can not accept an answer until they get at least 10 points.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges