C
C#2y ago
Elmishh

❔ making something wait in unity script

trying to make something wait 0.2 seconds but i just dont know how
51 Replies
Elmishh
ElmishhOP2y ago
tried StartCoroutine but i get Argument 1: cannot convert from 'method group' to 'string'
Zerthz
Zerthz2y ago
don't really know unity script. In standard c# you could have something like Task.Delay() https://gamedevbeginner.com/how-to-delay-a-function-in-unity/ does this article help any?
John French
Game Dev Beginner
How to delay a function in Unity - Game Dev Beginner
Learn how to delay a function, pause logic and trigger code at fixed intervals in Unity.
FusedQyou
FusedQyou2y ago
You can also do this in Unity Unity supports async-await fine Please share the script. Looks like your syntax is not what it's supposed to be Or, you know, look up how Coroutines work since there's plenty of documentation
Elmishh
ElmishhOP2y ago
The unity script document didnt work
FusedQyou
FusedQyou2y ago
Share the script We don't know what you did
FusedQyou
FusedQyou2y ago
StartCoroutine(colorChangeDelay); is not how you call a Coroutine
Elmishh
ElmishhOP2y ago
is it not? how do you call it?
FusedQyou
FusedQyou2y ago
You need to invoke the method and internally IEnumerator-returning methods will invoke your code until a Yield instruction is met. In your case that's new WaitForSeconds(0.2f); Unity handles these when you use StartCoroutine So if you change that to StartCoroutine(colorChangeDelay()); it works
Elmishh
ElmishhOP2y ago
so what should i do oh
FusedQyou
FusedQyou2y ago
Alternative:
var routine = colorChangeDelay();
StartCoroutine(routine);
var routine = colorChangeDelay();
StartCoroutine(routine);
Elmishh
ElmishhOP2y ago
why is unity docs so overly complex well do you know why the color isnt changing? its static red for some reason
FusedQyou
FusedQyou2y ago
private IEnumerator coroutine;
...

coroutine = WaitAndPrint(2.0f);
StartCoroutine(coroutine);
private IEnumerator coroutine;
...

coroutine = WaitAndPrint(2.0f);
StartCoroutine(coroutine);
They basically did the same thing as me You were probably confused by the extra parameter The docs are kinda shit here, yeah
Elmishh
ElmishhOP2y ago
i cant even do that i cant do coroutine = WaitAndPrint or in my case colorChangeDelay =
FusedQyou
FusedQyou2y ago
It's WaitAndPrint() WaitAndPrint equals a delegate
Elmishh
ElmishhOP2y ago
wdym
FusedQyou
FusedQyou2y ago
var routineInvoker = WaitAndPrint;
var routine = routineInvoker();
StartCoroutine(routine);
var routineInvoker = WaitAndPrint;
var routine = routineInvoker();
StartCoroutine(routine);
You would be saving a reference to the method Called a delegate
Elmishh
ElmishhOP2y ago
well do you know why the color isnt changing? its static on red
FusedQyou
FusedQyou2y ago
Because you probably misunderstand the point of Coroutines
Elmishh
ElmishhOP2y ago
im using them for delay
FusedQyou
FusedQyou2y ago
void Update()
{
if (isInizialized)
{
GorillaTagger.Instance.offlineVRRig.mainSkin.material.color = new Color(color1, color2, color3);
if (color1 < 255 && color2 == 0 && color3 == 0) { color1 += 28.3333333333f; }
if (color2 < 255 && color1 == 255) { color2 += 28.3333333333f; }
if (color3 < 255 && color2 == 255) { color3 += 28.3333333333f; }

if (color1 <= 255 && color1 > 0 && color3 == 255) { color1 -= 28.3333333333f; }
if (color2 <= 255 && color2 > 9 && color1 == 0) { color2 -= 28.3333333333f; }
if (color3 <= 255 && color3 > 9 && color2 == 0) { color3 -= 28.3333333333f; }
StartCoroutine(colorChangeDelay());
}
}
void Update()
{
if (isInizialized)
{
GorillaTagger.Instance.offlineVRRig.mainSkin.material.color = new Color(color1, color2, color3);
if (color1 < 255 && color2 == 0 && color3 == 0) { color1 += 28.3333333333f; }
if (color2 < 255 && color1 == 255) { color2 += 28.3333333333f; }
if (color3 < 255 && color2 == 255) { color3 += 28.3333333333f; }

if (color1 <= 255 && color1 > 0 && color3 == 255) { color1 -= 28.3333333333f; }
if (color2 <= 255 && color2 > 9 && color1 == 0) { color2 -= 28.3333333333f; }
if (color3 <= 255 && color3 > 9 && color2 == 0) { color3 -= 28.3333333333f; }
StartCoroutine(colorChangeDelay());
}
}
Do you expect this to delay the length that colorChangeDelay is waiting for?
Elmishh
ElmishhOP2y ago
no i just want it to wait 0.2 seconds every times it runs through so its not too fast
FusedQyou
FusedQyou2y ago
A Coroutine sort of runs in the background, so there is no context that simulates a delay here
Elmishh
ElmishhOP2y ago
ah
FusedQyou
FusedQyou2y ago
It's hard to explain, but your Coroutine does have a delay This code in Update simply does not have that
Elmishh
ElmishhOP2y ago
im confused now
FusedQyou
FusedQyou2y ago
What you can do, it put this code in a seperate Coroutine Give me a second
private _colorChangeWaitInstruction = new WaitForSeconds(0.2f);
void Start()
{
StartCoroutine(ColorChangeRoutine());
}

IEnumerator ColorChangeRoutine()
{
while(true)
{
if (!isInizialized)
{
// Wait a frame and check again.
// This is important to do to avoid it locking up!
yield return null;
continue;
}

GorillaTagger.Instance.offlineVRRig.mainSkin.material.color = new Color(color1, color2, color3);
if (color1 < 255 && color2 == 0 && color3 == 0) { color1 += 28.3333333333f; }
if (color2 < 255 && color1 == 255) { color2 += 28.3333333333f; }
if (color3 < 255 && color2 == 255) { color3 += 28.3333333333f; }

if (color1 <= 255 && color1 > 0 && color3 == 255) { color1 -= 28.3333333333f; }
if (color2 <= 255 && color2 > 9 && color1 == 0) { color2 -= 28.3333333333f; }
if (color3 <= 255 && color3 > 9 && color2 == 0) { color3 -= 28.3333333333f; }
yield return StartCoroutine(colorChangeDelay);
}
}

IEnumerator ColorChangeDelay()
{
yield return this._colorChangeWaitInstruction();
}
private _colorChangeWaitInstruction = new WaitForSeconds(0.2f);
void Start()
{
StartCoroutine(ColorChangeRoutine());
}

IEnumerator ColorChangeRoutine()
{
while(true)
{
if (!isInizialized)
{
// Wait a frame and check again.
// This is important to do to avoid it locking up!
yield return null;
continue;
}

GorillaTagger.Instance.offlineVRRig.mainSkin.material.color = new Color(color1, color2, color3);
if (color1 < 255 && color2 == 0 && color3 == 0) { color1 += 28.3333333333f; }
if (color2 < 255 && color1 == 255) { color2 += 28.3333333333f; }
if (color3 < 255 && color2 == 255) { color3 += 28.3333333333f; }

if (color1 <= 255 && color1 > 0 && color3 == 255) { color1 -= 28.3333333333f; }
if (color2 <= 255 && color2 > 9 && color1 == 0) { color2 -= 28.3333333333f; }
if (color3 <= 255 && color3 > 9 && color2 == 0) { color3 -= 28.3333333333f; }
yield return StartCoroutine(colorChangeDelay);
}
}

IEnumerator ColorChangeDelay()
{
yield return this._colorChangeWaitInstruction();
}
Something like this Notice you can use the delay routine in another Coroutine to simulate the delay You just can't do it where you did. These are methods that run on the main thread and they should never have a delay (because if they do, your program locks up), because this main thread does everything in your program (like rendering the screen).
Elmishh
ElmishhOP2y ago
wut
FusedQyou
FusedQyou2y ago
Either way, try this
Elmishh
ElmishhOP2y ago
im beyond confused
FusedQyou
FusedQyou2y ago
Your old code would run the code every single frame because it's in Update, and the delay didn't work either Unity is not going to wait for it to run
Elmishh
ElmishhOP2y ago
line 1: 7 errors 6
FusedQyou
FusedQyou2y ago
You should put a Debug.Log message in the code to see when it runs, then you can see what it does. Like: Debug.Log("I finished running"); at the end of the method, below the StartCoroutine()
Elmishh
ElmishhOP2y ago
FusedQyou
FusedQyou2y ago
What is the error?
Elmishh
ElmishhOP2y ago
just put in line 1
FusedQyou
FusedQyou2y ago
Can you screenshot your editor?
Elmishh
ElmishhOP2y ago
wdym
FusedQyou
FusedQyou2y ago
Screenshot your whole editor I want to see how it looks like
Elmishh
ElmishhOP2y ago
the code?
FusedQyou
FusedQyou2y ago
Yes You probably did not configure it properly
Elmishh
ElmishhOP2y ago
do you mean like this or something?
FusedQyou
FusedQyou2y ago
Small mistake in the code, change it to private WaitForSeconds _colorChangeWaitInstruction = new WaitForSeconds(0.2f); You also need to change yield return this._colorChangeWaitInstruction(); to yield return this._colorChangeWaitInstruction;
Elmishh
ElmishhOP2y ago
its still just static red
FusedQyou
FusedQyou2y ago
Then that has to do with your code Try the Debug.Log and see if it delays properly And you can also put your color values in it to see what it becomes It's most likely not what you expect it to be
Elmishh
ElmishhOP2y ago
its stuck at 283.3333, 0, 0 which i dont know why i know why wait actually i dont know why no idea color1 reaches 255 and goes to 283.3333 and stays there it for some reason doesnt stop at 255 well it changed colors now it went from red->yellow->white->stuck which is still not correct
FusedQyou
FusedQyou2y ago
Hmmmm Well it increments one more time, so I'm guessing your if-statement is wrong Perhaps your color is not exactly 255, and it's just below it, causing it to increment once more? You're comparing floating points, which can be inaccurate You can always use a Clamp on the colors to clamp them between 0-255
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server