Simple Coroutine Question

Can StartCoroutine be used by passing Class 1 into Class2.Function(Class1) and then calling StartCoroutine(Class1.SomeEnumeratorFunction) ? I know its a simple question but i’m getting an error: “Cannot access a non-static member of outer type ‘UnityEngine.MonoBehaviour’ via nested type ‘Somefunction’”

I need to call my function from within OnGUI. Example code:

using UnityEngine;
using System.Collections;

public class CoroutineTest : MonoBehaviour
{

// Use this for initialization
void Start ()
{

	SampleTestClass SampleTest = new SampleTestClass();
	
	HoldCoroutineClass HoldCoroutine = new HoldCoroutineClass();
	
	SampleTest.SomeFunction(HoldCoroutine);
	
}		

public class SampleTestClass
{
	public void SomeFunction(HoldCoroutineClass HoldCoroutine)
	{
		StartCoroutine(HoldCoroutine.SomeEnumFunction());
	}
}	

public class HoldCoroutineClass
{
	
	public IEnumerator SomeEnumFunction()
	{
		yield return null; 
	}
}	

}

You have the Somefunction and SomeEnumFunction in the same class? Check spelling(names are caps sensitive) and try:

StartCoroutine("corName", optionalValue)

Make IEnumerator public, check that you have

using System.Collections;
using UnityEngine;

At top of the script.

Check that your class inherit from MonoBechaviour

public class className : MonoBechaviour

Hope that helps

Paul

@mythraend: Will check that, and edit answer.
EDIT:
So, I tested it and I can normally start coroutine via my function, here is the code

using UnityEngine;
using System.Collections;

public class cortest : MonoBehaviour {
	
	public void someFunc()
	{
		StartCoroutine("cor");
	}
	
	public IEnumerator cor()
	{
		yield return null;
	}
}

Your code doesn’t work because Unity Coroutines are run on the GameObject. If there is no GameObject to run the coroutine to run on, one cannot use coroutines. To fix your example code, make SampleTestClass extend MonoBehaviour, and use AddComponent to add it to a gameobject instead of using the new keyword to make an instance.

Let’s say you have two GameObjects: GO1 & GO2. Let’s also, for the sake of simplicity, assume you can access the GameObjects by their names. Now, if within GO1 you have a script that says StartCoroutine( MyFunction() ); that coroutine would be run on GO1. This means if GO1 is disabled, so is the execution of the Coroutine.

If you happen to have a need to disable GO1, but keep the coroutine active, you need to run it on a different GameObject. You can achieve it by writing in a script in GO1: GO2.GetComponent< CoroutineHandler >.StartCoroutine( MyFunction() );. CoroutineHandler being a MonoBehaviour-extending class attached to GO2.

Thank you! It was good to see the door creak open using the coroutine call.

It’s for a rpg with a need for lots of animations that need to run in coroutines. So, I think I should embed them from a centralized GUI call. I just got the main script object with getcomponent (probably inefficient but good enough for now). I appreciate both of your help, Jamora and fapawei.