So… Hi, I was searching for this in some of the Unity Answers, but I couldn’t find, what I needed… So I need something, that will delay ANY action, like:
Have you looked at Coroutines? This is exactly what they’re for.
Update is designed to be executed, start to finish, within one frame (this is actually the function that defines what a frame is). This means that it doesn’t make much sense to have Update span multiple frames.
What you want to do is have update start a Coroutine that handles delaying these sounds. This does mean you have to manage that Coroutine so you don’t start a new one every frame, but this is pretty standard.
It sounds to me that what your looking for is a Coroutine. They are able to do just that, using some of the following:
WaitForSecondsRealtime(float);
WaitUntil (() => (delegate) );
WaitForEndOfFrame();
and a few others, including simply null.
It is used like this:
yield return new WaitForSecondsRealtime(3f); //will wait 3 seconds before continuing this loop.
Though since this is for audio, I would suggect maybe instead of specifying 3f, you could specify something like yield return new WaitForSecondsRealtime(sound1.length); so when that clip is done playing, itll move on - or even yield return new WaitUntil(() => sound1.isPlaying == false)
If you really wanted to use Update for this, I would first suggest to use LateUpdate or FixedUpdate as their more accurate and fixed to approximately 1 second, far better than Update is. Then I would suggest to create a public “time” variable, you can use and a “max value” to know when to stop. Something like:
Which as you can see, is a little bit more messy than a coroutine, given, it can be cleaned up a bit by creating a function to call instead thatll do that code, but it sounds like a coroutine will do what you want a lot better, I would suggest to look into them a little more - and try to avoid making infinite loops (so while(true){} for example)