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 Ryutsashi · Apr 25, 2014 at 06:23 AM · javascriptcoroutine

Coroutine gets skipped in javascript/unityscript

The class is instantiated in a different script:

 sunRotation = new AutoRotate(sunContainer, dayLength * 60, Vector3.up);

, the constructor calls the coroutine and nothing happens. Debugging shows coroutine being called, it goes over to the line where the coroutine starts, then jumps back to the constructor and finishes executing never returning to the coroutine or even running anything inside it.

I tried putting the coroutine inside of the class code. I tried making it private/public. Even after looking at tutorials dealing with classes and coroutines and reading through the forums I'm still unable to see what I'm doing wrong. :(

This is the watered down version of the code (if you need more information, look below for the ENTIRE CLASS code):

 #pragma strict
 
 public class AutoRotate {
     public function AutoRotate (parameters) {
         //Calls the coroutine
         RotateCoroutine(parameters); //Goes to the coroutine and jumps back
         //Continues here
     }
 }
 
 function RotateCoroutine (parameters) { //This line executes and then it jumps back to the origin point
     while (something) { //This line NEVER GETS REACHED
         //code
         yield WaitForSeconds(someFloatVariable);
     }
     //This line NEVER GETS REACHED either
 }

This is the whole class code here, nothing held back:

 #pragma strict
 
 public class AutoRotate {
     private var priority : float = 0.25;
     public function set Priority (value : float) {
         priority = value;        //Assets/AutoRotate.js(6,28): BCW0028: WARNING: Implicit downcast from 'Object' to 'float'.
         if (priority < 0.0) {
             Debug.Log("AutoRotate > 'priority' can't be '" + priority + "'! Corrected to 0.25!");
             priority = 0.25;
         }
     }
 
     public var isActive : boolean = true;
 
     public function AutoRotate (object : GameObject) {
         AutoRotate (object.transform, 4, Vector3.up);
     }
     public function AutoRotate (object : Transform) {
         AutoRotate (object, 4, Vector3.up);
     }
     public function AutoRotate (object : GameObject, rotationTime : float, rotationAxis : Vector3) {
         AutoRotate (object.transform, rotationTime, rotationAxis);
     }
 
     public function AutoRotate (object : Transform, rotationTime : float, rotationAxis : Vector3) {
         RotateCoroutine(object, rotationTime, rotationAxis);
     }
 }
 
 function RotateCoroutine (object : Transform, rotationTime : float, rotationAxis : Vector3) {
     while (isActive) {
         if (!object) Debug.Log("AutoRotate > RotateCoroutine > 'object' is 'NULL'.");
         Debug.Log(rotationAxis);
         object.transform.Rotate(rotationAxis, 360 / priority / rotationTime);
         yield WaitForSeconds(priority);
     }
 }
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 robertbu · Apr 25, 2014 at 06:30 AM 1
Share

I don't believe you can have a Coroutine in a class not derived from $$anonymous$$onobehaviour. As a test, make the class derived from $$anonymous$$onobehaviour and then dynamically create a game object and attach the script with AddComponent().

avatar image Ryutsashi · Apr 25, 2014 at 09:39 AM 0
Share

Thanks :D It works. There were more errors afterwards but I got it working. However, I'm not quite content with the way I did it. I might just try to implement a factory function ins$$anonymous$$d... maaaaaaybe.

Anyways, thanks a lot. I've been trying to tackle it for hours on end :D

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

yield works, but yield WaitForSeconds does not? 1 Answer

Question about Coroutines in a Class Function 1 Answer

C# to UnityScript conversion help. 0 Answers

Updating a TextMesh buggy? 1 Answer

Stuck while solving a Coroutine problem 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