简体   繁体   中英

Unity C# StartCoroutines - is it possible to put a string into the StartCoroutine

I'm using StartCoroutines in a different class, problem is I need the class its calling to be dynamic. LevelSelected = "Camera.GetComponent<Level"+levelnumber+">().Pattern1()";

LevelSelected is a string,the levelnumber is the level(int), and I'm wanting it to pick up the enumerator functions from that class. Level number is decided by whichever level the player picks on the start menu.

Problem I'm having is... if I output what I need into the string.

`StartCoroutine (LevelSelected);`

It errors out saying - Coroutine 'Camera.GetComponent().Pattern2()' couldn't be started! But when I hardcode this value in, it works fine...

is there a way around this??

Thanks, Craig

I'm fairly sure you are performing an illegal action right there GetComponent<> can only use direct references to class names. Instead of trying to throw in the value within the string, do something like this :

var levelScript = Camera.GetComponent<Level>();
StartCoroutine(levelScript.Pattern(levelnumber));

And in your pattern function change it to something like this

IEnumerator Pattern(int levelnumber)
{

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM