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 awdawdawd · Aug 16, 2012 at 01:17 PM · loopyieldwaitforsecondswhile

Yield Not Working - While Loop

Hello,

I'm completely confused in why my while loop wont work, all its suppose to do is change my object material from on to another, over and over again.

 function Start(){
      AnimateElectic();
 }
 
 function AnimateElectic(){
     
     print("Animating Shock!");
     
     while(true){
             
             //yield WaitForSeconds(1);//Random.Range(0.0, 5.0));
             bat.renderer.material = levelTextures[3];
             print("On");
             //electric = true;
             //yield WaitForSeconds(1);//Random.Range(0.0, 5.0));
             
             yield WaitForSeconds(0.5);
             
             //yield WaitForSeconds(1);//Random.Range(0.0, 0.8));
             bat.renderer.material = levelTextures[2];
             print("Off");
             //electric = false;
             //yield WaitForSeconds(1);//Random.Range(0.0, 0.8));
             
             yield WaitForSeconds(0.5);
 
     }
     
     //animateEffect = false;
 }

But it just doesn't work, it does the first section - so it changes the material to levelTexture[3], prints the 'On', but seems to fail at the yield? I've used loads the loops before, and yet this one doesn't seem to work? Am I being stupid here?

EDIT:

I forgot to add that the object is Instantiated at a certain point. If I start the game with the object already in the scene, active, then the loop works. But when I instantiate it at a later stage in the game, it only does the first section, once.

Thanks

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Kryptos · Aug 16, 2012 at 02:13 PM

Start is only called once. So if your object is active, then you deactivate it then you activate it again, Start will not be called again.

Consider calling the coroutine in OnEnable() instead of Start().

Comment
Add comment · Show 2 · 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 awdawdawd · Aug 16, 2012 at 02:22 PM 0
Share

Ah wow, I had no idea there was such a thing as 'OnEnable'. Thanks!

avatar image Kryptos · Aug 16, 2012 at 02:36 PM 1
Share

These two pages of the documentation can be really useful:

  1. $$anonymous$$onoBehaviour class

  2. Execution Order of Event Functions

avatar image
-2

Answer by shinriyo_twitter · Apr 25, 2013 at 06:18 PM

UnityScript is bad, C# is better.UnityScript is redundancy

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
avatar image
0

Answer by SolidSnake · Aug 16, 2012 at 01:39 PM

Do you get an error log?...

I am coding mostly in C#, but don't you need to call the function like this in JavaScript:

 StartCoroutine(AnimateElectic());
Comment
Add comment · Show 2 · 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 awdawdawd · Aug 16, 2012 at 01:40 PM 0
Share

I get no error log, and like I said, it works - but only when the object is already active in the game. But thats not what I want, its already in the scene, but not active until a certain game trigger.

avatar image SolidSnake · Aug 16, 2012 at 01:46 PM 0
Share

and by not active you mean not instantiated? or simply you are setting active property to false?

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

11 People are following this question.

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

Related Questions

Simple rotation script. (loop) 3 Answers

Definition of Script.function() depends on Script.function()... 3 Answers

A delay/yield/wait function for function Update? 6 Answers

while loop not looping 2 Answers

Loop animation with a wait in between 3 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