- Home /
Calling a coroutine when the IENumerator has a passing argument?
Trying to call a coroutine, but I get an error: " Unexpected symbol "other" " in the start method
void Start()
{
StartCoroutine(OnTriggerEnter(Collider other));
}
Coroutine:
IEnumerator OnTriggerEnter(Collider other)
{
if (other.tag == "Pickup")
{
Vector3 spawnPlusPoint = new Vector3(0,0,0);
plusPoint.text = "+3";
Instantiate(plusPoint, spawnPlusPoint, Quaternion.identity);
yield return new WaitForSeconds(1.5f);
plusPoint.text = "";
}
}
Answer by Bunny83 · Oct 06, 2014 at 12:16 AM
You have several things wrong here. First of all when calling a method (no matter if it's a "normal" method or a generator method, an IEnumerator) you have to pass the parameters the method requires in it's declaration. Inside your Start method you don't pass a variable, it looks like you just copied the parameter declaration which makes no sense at all.
Your second problem is OnTriggerEnter is a callback that is called by Unity automatically. Usually it makes no sense to call it manually. Even if you want to call it manually you should make yourself clear what collider you want to pass to the function. Are you sure you know what you actually want to do? And if so could you please explain that in your question?
If you want to start the coroutine manually you have to call it like this:
StartCoroutine(OnTriggerEnter(someColliderReference));
where "someColliderReference" is the collider you want to pass.
Wow, thanks man. Thought you always had to start a coroutine for it to work, I just took out that line from the Start method and it works fine. Would rep but I can't because I'm new.
Thanks
Answer by OctoMan · Oct 06, 2014 at 12:23 AM
Starting the coroutine with a value(string,int,bool, float and so on), is what you have to do normally.
example: StartCoroutine(OnTriggerEnter(1.0f));
and in IEnumerator OnTriggerEnter(float value) to make use of the value inside of the IEnumerator.
like: yield return new waitforseconds(value);
So you might want to store the collider somehow in a variable. Not sure if thats possible.