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:
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 convertmethod group' expression to type
System.Collections.IEnumerator'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 avariable',
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!
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...
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 :)
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());
}
}
Little did you know you helped someone 4 years later, thank you!