C
C#15mo 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
Elmishh15mo ago
tried StartCoroutine but i get Argument 1: cannot convert from 'method group' to 'string'
Zerthz
Zerthz15mo 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
FusedQyou15mo 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
Elmishh15mo ago
The unity script document didnt work
FusedQyou
FusedQyou15mo ago
Share the script We don't know what you did
FusedQyou
FusedQyou15mo ago
StartCoroutine(colorChangeDelay); is not how you call a Coroutine
Elmishh
Elmishh15mo ago
is it not? how do you call it?
FusedQyou
FusedQyou15mo 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
Elmishh15mo ago
so what should i do oh
FusedQyou
FusedQyou15mo ago
Alternative:
var routine = colorChangeDelay();
StartCoroutine(routine);
var routine = colorChangeDelay();
StartCoroutine(routine);
Elmishh
Elmishh15mo ago
why is unity docs so overly complex well do you know why the color isnt changing? its static red for some reason
FusedQyou
FusedQyou15mo 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
Elmishh15mo ago
i cant even do that i cant do coroutine = WaitAndPrint or in my case colorChangeDelay =
FusedQyou
FusedQyou15mo ago
It's WaitAndPrint() WaitAndPrint equals a delegate
Elmishh
Elmishh15mo ago
wdym
FusedQyou
FusedQyou15mo 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
Elmishh15mo ago
well do you know why the color isnt changing? its static on red
FusedQyou
FusedQyou15mo ago
Because you probably misunderstand the point of Coroutines
Elmishh
Elmishh15mo ago
im using them for delay
FusedQyou
FusedQyou15mo 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
Elmishh15mo ago
no i just want it to wait 0.2 seconds every times it runs through so its not too fast
FusedQyou
FusedQyou15mo ago
A Coroutine sort of runs in the background, so there is no context that simulates a delay here
Elmishh
Elmishh15mo ago
ah
FusedQyou
FusedQyou15mo ago
It's hard to explain, but your Coroutine does have a delay This code in Update simply does not have that
Elmishh
Elmishh15mo ago
im confused now
FusedQyou
FusedQyou15mo 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
Elmishh15mo ago
wut
FusedQyou
FusedQyou15mo ago
Either way, try this
Elmishh
Elmishh15mo ago
im beyond confused
FusedQyou
FusedQyou15mo 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
Elmishh15mo ago
line 1: 7 errors 6
FusedQyou
FusedQyou15mo 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
Elmishh15mo ago
FusedQyou
FusedQyou15mo ago
What is the error?
Elmishh
Elmishh15mo ago
just put in line 1
FusedQyou
FusedQyou15mo ago
Can you screenshot your editor?
Elmishh
Elmishh15mo ago
wdym
FusedQyou
FusedQyou15mo ago
Screenshot your whole editor I want to see how it looks like
Elmishh
Elmishh15mo ago
the code?
FusedQyou
FusedQyou15mo ago
Yes You probably did not configure it properly
Elmishh
Elmishh15mo ago
do you mean like this or something?
FusedQyou
FusedQyou15mo 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
Elmishh15mo ago
its still just static red
FusedQyou
FusedQyou15mo 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
Elmishh15mo 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
FusedQyou15mo 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
Accord15mo 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
More Posts
✅ Parse .md like .json in C# classDoes anyone know how I can parse a "settings markdown" file into a C# class, like I could do with ne❔ Invalid remote certificate, error in certificate chain: Partial chainWhen I am sending this GET request from my machine in VS, everything works perferctly normal. When I❔ how to find the variable in an equation of a known integral?For instance hypothetically, say you have the equation 3x+b and the integral is known to be 30 in th❔ I have this serviceWorker running Selenium for screenshots and its not working properly.I need help identifying my problem and i have reasons to believe there is a leak somewhere in the co❔ WPF Converter not triggerHello, i do not understand there why my converter isn't trigger. Have you any idea what can be the p❔ Need Help with Rotation for my 3D modelHi, i created a project in c# to apply Verlet integration on some OBJ file. In the video is the v❔ My datagrid view is not refreshing instantly (WinForms)After doing the button update function and populating the database, the data grid view is supposed t❔ C# Help Doubt Resource1.resxHello everyone, I'm a beginner in C#, I have a doubt, I created a new file called Resource1.resx for✅ object types?hey ive been following this video on how to make tictactoe, for a class assignment, (https://youtu.b❔ Azure Function dynamic queue output binding for Isolated Process?Hi friends, I am trying to see if isolated process mode can do dynamic queues. Here is a nice articl