how am i supposed to return a float value from IEnumerable function?
IEnumerable is a collection of items, so you cannot convert it to a single item. You should get its enumerator and iterate it in a while loop using MoveNext() and Current:
IEnumerator<float> myEnumerator = myEnumerable.GetEnumerator();
while (myEnumerator.MoveNext())
{
float i = myEnumerator.Current;
}
PS: you are talking about a “function”, may be you can add some details about what you need, of course you cannot return a result type different from the function type itself
Or simply use a foreach which is more readable IMHO
foreach (float myFloat in myEnumerable)
{
..
}
public static IEnumerator Alarm(float total) {
float v = 0.0f;
while (v <= total) {
v += Time.deltaTime;
yield return v;
}
}
I want to get the value of v returned as a float in the above function
Not sure abou what exactly you want. Could you give a sample in “pseudocode”?
I want a static countdown alarm function which starts its own loop when needed
Alarm(total seconds) {
float currentseconds = 0
while currentseconds < total seconds
currentseconds++
return currentseconds
endwhile
}
I am sorry but I can´t help you with this.
Does this Help?
public void DoStuff()
{
IEnumerator<float> enumerator = Alarm(10.0f);
while(enumerator.MoveNext())
{
float t = enumerator.Current;
Debug.Log(t);
//Do Stuff here between calls with the float value t, or instead of using while loop pass Enumerator to Next frame
}
//Loop is Done and timer is out.
}
public static IEnumerator<float> Alarm(float total) {
float v = 0.0f;
while (v <= total) {
v += Time.deltaTime;
yield return v;
}
}
}
You will probably have to wait a frame, or else DeltaTime will always be the same, though you can use Time.realTimeSInceStartup to get the Time delta within a single frame. Just store the base value and get a new value and subtract the base to get the delta.
Note: It’s very similar to the one above, if you explain how you plan to yield or what that function is designed to accomplish, maybe it could either use the existing Unity Coroutine system, or we could help give even clearer direction, i.e. what do you want to do with v, and how is this function supposed to operate with regards to the main loop (blocking or waiting on X process, or waiting on frames).