The question is answered, right answer was accepted
StartCoroutine & WaitForSeconds Error
I'm currently working on a clicker game but when ever I try and use a StartCoroutine for my autoticker it gives me the following two errors:
1: Assets/Scripts/GoldPerSec.cs(12,17): error CS1502: The best overloaded method match for UnityEngine.MonoBehaviour.StartCoroutine(System.Collections.IEnumerator) has some invalid arguments
2: Assets/Scripts/GoldPerSec.cs(12,17): error CS1503: Argument #1 cannot convert System.Collections.IEnumerable expression to type `System.Collections.IEnumerator'
using UnityEngine;
using System.Collections;
public class GoldPerSec : MonoBehaviour {
public UnityEngine.UI.Text gpsDisplay;
public Click click;
public ItemManager[] items;
// Use this for initialization
void Start () {
StartCoroutine(AutoTick());
}
// Update is called once per frame
void Update () {
gpsDisplay.text = GetGoldPerSec() + " gold/sec";
}
public int GetGoldPerSec(){
int tick = 0;
foreach(ItemManager item in items){
tick += item.count * item.tickValue;
}
return tick;
}
public void AutoGoldPerSec(){
click.gold += GetGoldPerSec();
}
IEnumerable AutoTick(){
while(true){
AutoGoldPerSec();
yield return new WaitForSeconds(1);
}
}
}
Answer by Dave-Carlile · Oct 12, 2015 at 03:39 PM
According to the StartCoroutine documentation, your Coroutine function should be returning IEnumerator, not IEnumerable. The error message is telling you that exact thing - it's trying to convert the IEnumerable that you're returning into an IEnumerator that StartCoroutine expects, and it can't do it because it's not a valid typecast.
Thank you so much, It silly things like this that alway get me.
Follow this Question
Related Questions
IENumerator does not work 0 Answers
Can Unity throw an error when I start a coroutine without StartCoroutine? 0 Answers
Trying to add a delay to an Instantiate in a For Loop 0 Answers
[C#] Coroutine just won't stop! 2 Answers
why the startcoroutine (spawnbigtree()); doesn't work and float don't work 0 Answers