• 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 AngryCSharpNerd · Dec 30, 2013 at 04:13 PM · staticwarningwarningsstatic-functionstatic script

Static function won't call another static function

If I try doing this:

 static function f() {
     go();
 }
  
 static function go() {
     yield;
 }

I will get an error saying:

UCW0004: WARNING: Coroutine 'go' cannot be automatically started from a static function.

Why can I not do this? The function will not even run, and my real code really needs to use a static function in a static function.

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

1 Reply

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

Answer by KellyThomas · Dec 30, 2013 at 04:18 PM

I think Unity coroutines need to be attached to a GameObject.

You will have no trouble calling static (non-coroutine) functions from static functions.

You can replace yield with a log statement for a simple test.

Comment
Add comment · Show 8 · 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 AngryCSharpNerd · Dec 30, 2013 at 04:23 PM 0
Share

Yeah, I just tried with a log, and it did work, but what do you mean by attaching a coroutine to a GameObject?

avatar image KellyThomas · Dec 30, 2013 at 04:32 PM 0
Share

I'm having trouble find a reference (someone else might have a good link) but the unity coroutines are very closely tied to GameObjects (i.e. they can't be started if their game object is inactive, they stop when the game object is set to inactive or destroyed). As such they are tied to that instance and can't be static functions.

avatar image AngryCSharpNerd · Dec 30, 2013 at 04:35 PM 0
Share

Oh.. like OnCollisionEnter[2D]?

avatar image KellyThomas · Dec 30, 2013 at 04:45 PM 0
Share

Well yeah, all of the $$anonymous$$onoBehaviour message functions are designed to apply to a single GameObject instance.

avatar image AngryCSharpNerd · Dec 30, 2013 at 04:53 PM 0
Share

Okay, I might sound like an idiot right here, but what should my solution be then? I'll give you a sample of my real code.

 static function PlaySound(obj:GameObject,x:AudioClip){
     var source = obj.AddComponent(AudioSource);
     source.clip = x;
     source.Play();
     while(source){
         if(!source.isPlaying){
             Destroy(source);
         }
         yield;
     }
 }
 
 static function Damage(amount:int,rig:Rigidbody2D,properties:Properties,pileUp:float,sounds:AudioClip[],sound1:int,sound2:int){
     properties.health -= amount;
     rig.velocity = new Vector2(rig.velocity.x,0);
     rig.AddForce(Vector3(2,2,0)*200/rig.mass);
     PlaySound(rig.gameObject,sounds[sound1]); //I need to play a sound using the static function from above
     PlaySound(rig.gameObject,sounds[sound2]);
     Invincible(0.5,properties,rig.gameObject,true,pileUp,0); //Not going to focus on this one for now, even though this is also a problem
 }
Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Data from a simple singleton / static class return null 1 Answer

When should i use static functions? 2 Answers

Calling class from monobehaviour class 1 Answer

why do i get this warining when there is a main camera? 0 Answers

Box Collider 2D -failed verification- warning even when the size is reasonable 2 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