- Home /
'StartCoroutine' does not exist in the current context
Hello, I'm converting my project from JS to C# and I believe this is the very last of the hundreds of errors I had to resolve in the process. I'm receiving the error "StartCoroutine' does not exist in the current context " on line 312.
In method:
public void OnControllerColliderHit(ControllerColliderHit hit) { [...]
component = gameObj.GetComponent("MovementReverse");
if(component != null)
{
MovementReverse handler;
handler = component as MovementReverse;
StartCoroutine(MovementReverse(handler)); // line 312 with the trouble
}
[...]
}
This is within my BlobController class:
[RequireComponent(typeof (CharacterController))]
public class BlobController : Component
And my file has the standard usings:
using UnityEngine;
using System.Collections;
Oddly, StartCoroutine appears in the intellisense in MonoDevelop as a method, however when typing the parenthesis '(' it doesn't display the typical box to let you know what arguments are required, which seems to imply that some library isn't properly referenced or something, however even the reference in the Unity manual doesn't have any additional using statements.
http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.StartCoroutine.html?from=index http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html
In addition, I've tried all of these lines, none work:
StartCoroutine(MovementReverse(handler));
yield return StartCoroutine(MovementReverse(handler)); // changed method return type to IEnumerator
StartCoroutine(new MovementReverse(handler));
yield return StartCoroutine(new MovementReverse(handler));
Am I doing it wrong? I'm stumped.
Edit 1: Here's the coroutine I'm calling:
public IEnumerator MovementReverse(MovementReverse handler) { // stop blob from moving StopMoving();
// wait, will resume this public void at this spot after 1 second
yield return new WaitForSeconds(handler.delayBeforeReversing);
[...]
}
According to the reference and some blog posts, this appears to be correct, what is going wrong?
Answer by Mike 3 · Dec 22, 2010 at 01:01 AM
BlobController should be MonoBehaviour not Component
Thanks $$anonymous$$ike, that did the trick! As a rule, should most things be $$anonymous$$onoBehaviour ins$$anonymous$$d of Component? Is there a simple way to distinguish between what should be what?
Your scripts should all be $$anonymous$$onoBehaviour really, though other classes that don't need attaching to gameobjects can be whatever you like
Answer by _Petroz · Dec 22, 2010 at 01:02 AM
Your class is:
public class BlobController : Component
The function you're trying to call is:
MonoBehaviour.StartCoroutine
Answer by dissidently · Dec 21, 2010 at 07:21 PM
Check this blog post:
http://www.blog.silentkraken.com/2010/01/23/coroutines-in-unity3d-c-version/
And the previous blog post refers to this guys use of coroutines in Java, so may help you figure out what's going on. My guess would be you need the
IEnumerator MyCoroutine()
part
actually read all this guys blog. He's verbosely brilliant and should post much more!!!!
I've already done all the things listed in this blog post. I edited the original post to indicate that I've already got my coroutine method declared as an IEnumerator return value, and I'm calling yield return new WaitForSeconds(etc.);
Answer by Ashkan_gc · Aug 08, 2011 at 08:12 PM
StartCoroutine is a member of MonoBehaviour class (a none static one) so your class must derrive from MonoBehaviour to be able to call/use it.