Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by nickostan · Feb 11, 2016 at 06:32 PM · coroutineaccessing scripts

Reference coroutine from another script

Hello,

I'm trying to get a function in my CharacterController script to reference a coroutine from another script.

Basically the flow is

on left mouse click --> check the selected skill --> begin casting skill via coroutine --> shoot the skill

Here is some sample code: I'm running into a myriad of issues:

SCRIPT 1;

 using UnityEngine;
 using System.Collections;
 
 public class CharControllerScript : MonoBehaviour {
 
 
     void Update () {
 
         if (Input.GetKeyDown (KeyCode.Mouse0)) {
             shoot ();
         }
     
     }
 
     public void shoot(){
 
         StartCoroutine (ClassSpecificSkills.shoot_FrostBolt);
 
     }
 }

SCRIPT 2:

 using UnityEngine;
 using System.Collections;
 
 public class ClassSpecificSkills : MonoBehaviour {
 
     public static IEnumerator shoot_FrostBolt(){
 
         for (float f = 10f; f > 0; f -= 0.1f) {
             Debug.Log ("Coroutine running 10 times");
         }
 
         yield return null;
 
     }
 
 }


As you can see, my first script (CharController) tries to start a coroutine which is written in a second script (classSpecificSkills). The error I'm getting is the following:

  1. Assets/CharControllerScript.cs(22,17): error CS1502: The best overloaded method match for UnityEngine.MonoBehaviour.StartCoroutine(System.Collections.IEnumerator)' has some invalid arguments 2. Assets/CharControllerScript.cs(22,17): error CS1503: Argument #1' cannot convert method group' expression to type System.Collections.IEnumerator'

  2. Assets/CharControllerScript.cs(24,17): error CS0127: CharControllerScript.shoot()': A return keyword must not be followed by any expression when method returns void 4. Assets/CharControllerScript.cs(24,24): error CS0119: Expression denotes a type', where a variable', value' or `method group' was expected

I've been struggling with this for a few days, trying to get the syntax correct for coroutines but keep going in circles.

Any help is appreciated. 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

2 Replies

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

Answer by gjf · Feb 11, 2016 at 06:31 PM

your StartCoroutine syntax is one of your problems - it should be:

 StartCoroutine (ClassSpecificSkills.shoot_FrostBolt());

also, the way it's written, it will display your debug text 100 times and then yield... you need to yield in the loop if you expect the count to be over many frames...

Comment
Add comment · Show 1 · 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 nickostan · Feb 11, 2016 at 09:28 PM 1
Share

Thank you so much, I'm such an idiot.

I understand I wrote the rest of it probably incorrectly, I just needed to get the damn thing running in the first place.

Can't believe it was a stupid syntax error.

Thanks again buddy :)

avatar image
0

Answer by nickostan · Feb 11, 2016 at 10:31 PM

The correct code, for anyone looking for this answer in the future:

COROUTINE STRUCTURE:

 public static IEnumerator shoot_FrostBolt(){
 
         for (float f = 10f; f > 0; f -= 1f) {
             Debug.Log (f + " seconds left");
             yield return new WaitForSeconds (1.0f);
         }
 
         Debug.Log ("FINISHED!");
 
     }


ACCESSING THE COROUTINE FROM A SECOND SCRIPT:

 public class CharControllerScript : MonoBehaviour {
 
     void Update () {
 
         if (Input.GetKeyDown (KeyCode.Mouse0)) {
             shoot ();
         }
     
     }
 
     public void shoot(){
 
         StartCoroutine (ClassSpecificSkills.shoot_FrostBolt());
 
     }
         
 }



Comment
Add comment · Show 1 · 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 ELRandomMan · Apr 08, 2021 at 12:17 AM 0
Share

Little did you know you helped someone 4 years later, thank you!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

WaitForSeconds doesn't wait if time is too low 0 Answers

Can you use a character controller in a coroutine? 0 Answers

Getting variable from script while keeping other code from it 1 Answer

How to do very accurate time measurement 3 Answers

Coroutine Issue 1 Answer


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