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 Smaika · Jan 31, 2015 at 07:12 PM · javascriptyieldyield waitforseconds

yield WaitForSeconds(5); IS WAITING FOR EVER

Here is my code

 function Start (){
 
     //Some Stuff is here
     yield WaitForSeconds(5);
 
     if(Hart1.GetComponent(SpriteRenderer).sprite == hart){
 
         All();
     }
     else if(Hart2.GetComponent(SpriteRenderer).sprite == hart){
 
              Two();
          }
 
     function All (){
         //some stuff 
     }

     function Two (){
         //some stuff 
     }

The problem is function All () never called I tried to put

  print("All Called");

in function All () but still it never called nor function Two() if I put it in there

What am doing wrong? How can I fix it?

Comment
Add comment · Show 2
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 meat5000 ♦ · Jan 31, 2015 at 02:25 PM 0
Share

You seem to be missing a } somewhere

avatar image HakanOzcan · Jan 31, 2015 at 09:25 PM 0
Share

put your waitForSeconds stuff in to a IEnumerator function and call it ins$$anonymous$$d of directly say waitForSeconds, just like

  function Start ()
 {
  
      //Some Stuff is here
      Wait();
  
      if(Hart1.GetComponent(SpriteRenderer).sprite == hart){
  
          All();
      }
      else if(Hart2.GetComponent(SpriteRenderer).sprite == hart){
  
               Two();
           }
 }
  
      function All (){
          //some stuff 
      }
  
      function Two (){
          //some stuff 
      }
 
     IEnumerator Wait()
     {
     yield return new WaitForSeconds (2);
     }


and yes you are missin a }:)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Mr. Mud · Feb 01, 2015 at 12:27 PM

The issue here might come from something different than the "WaitForSeconds(5)". You said that you tried putting a Debug in either function which can be called, but neither of them printed anything. These functions however only get executed when a certain condition is met: the sprite in the sprite renderer equals hart. If neither of them is equal, none of the functions will be called.

I would suggest to add some more debug logs to your code, as this can give a hint on where the issue arises. Here is a sample:


 #pragma strict
 
 var hart1:GameObject;
 var hart2:GameObject;
 
 var hart:Sprite;
 
 function Start () 
 {
     //If this debug does not show, your script is most likely disabled.
     Debug.Log("Before yield");
     yield WaitForSeconds(5);
     
     //Yield did not give any problems; it is five seconds later.
     Debug.Log("5 seconds later.");
     
     //NOTE: Make sure you assigned objects and they have a SpriteRenderer attached to them.
     //If not, the next piece of code will result in NullPointerExceptions.
     if (hart1.GetComponent(SpriteRenderer).sprite == hart)
     {
         Debug.Log("Hart1 equals hart");
         All();
     }
     else if (hart2.GetComponent(SpriteRenderer).sprite == hart)
     {
         Debug.Log("Hart2 equals hart");
         Two();
     }
     else
     {
         //Show the names of the assigned sprites. to figure out where it went wrong.
         Debug.Log("hart1: " + hart1.GetComponent(SpriteRenderer).sprite);
         Debug.Log("hart2: " + hart2.GetComponent(SpriteRenderer).sprite);
         Debug.Log("hart: " + hart);
     }
 }
 
 function All()
 {
     Debug.Log("Entered All");
 }
 
 function Two()
 {
     Debug.Log("Entered Two");
 }

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Yield Waitforseconds not working at all 3 Answers

yield WaitForSeconds preventing further actions. 1 Answer

Problem with yield WaitForSeconds (I think) -javascript) 1 Answer

Function with itself called inside it. (JavaScript) 2 Answers

Walking sound change 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