Trigger an Action after some Time (Unity Tutorial)

  Рет қаралды 59,896

Code Monkey

Code Monkey

5 жыл бұрын

Let's see how we can trigger an Action after some time, extremely useful for cutscenes.
✅ Get the Project files and Utilities at unitycodemonkey.com/video.php...
If you have any questions post them in the comments and I'll do my best to answer them.
🔔 Subscribe for more Unity Tutorials / @codemonkeyunity
See you next time!
📦 Grab the game bundle at unitycodemonkey.com/gameBundl...
📝 Get the Code Monkey Utilities at unitycodemonkey.com/utils.php
#unitytutorial #unity3d #unity2d
--------------------------------------------------------------------
Hello and welcome, I am your Code Monkey and here you will learn everything about Game Development in Unity 2D using C#.
I've been developing games for several years with 7 published games on Steam and now I'm sharing my knowledge to help you on your own game development journey.
You can see my games at www.endlessloopstudios.com
--------------------------------------------------------------------
- Website: unitycodemonkey.com/
- Twitter: / unitycodemonkey
- Facebook: / unitycodemonkey

Пікірлер: 139
@indieprogress7170
@indieprogress7170 5 жыл бұрын
This is very helpful. Thanks! I usually use coroutines too, but this seems to be much simpler. I might try this on my crops and avoid the day/night cycle. Thanks, again.
@TheKr0ckeR
@TheKr0ckeR 2 жыл бұрын
I really like your modular works and try to do it my own way for my scripts! You inspire me, thank you.
@slipknotsoad86
@slipknotsoad86 2 жыл бұрын
Fantastic, I've used this as a pause between turns for a roguelike movement so it gives a .3 pause before user can move again. Makes the movement feel much better.
@scottblemaster744
@scottblemaster744 3 жыл бұрын
Extremely clever & useful piece of code. Thank you for this & all of your extremely helpful videos. You are a life saver.
@KillerGameDev
@KillerGameDev Жыл бұрын
This is really cool. I did something SLIGHTLY different for the destroy thing. I just put my timer in a variable if I plan to kill it. Then I reference the variable and use the destroy functionality to kill it. Im hoping to avoid the for loop but I get how it could come in handy sometimes. Thanks for sharing this. :D Also, been watching you for over a year and even get your email newsletter but just realized I have not subscribed to your channel. Fixed that situation right now. haha. Thanks again for all the cool stuff you share!
@luka_17_63
@luka_17_63 Жыл бұрын
Wow wow, I did calling functions triggert by time in such a weird way coparted to yours. I think now everything will be more understandable and also cleaner in my code. Thy @CodeMonkey as always you are teaching me new stuff to get my dream near to be a professional developer for Games or anything else. Big Heart for you!
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
I'm glad you found the video helpful! Thanks!
@vrtech473
@vrtech473 3 жыл бұрын
This is so helpful Thank you !
@raven5165
@raven5165 5 жыл бұрын
Man , it is very nice to see your channel growing
@CodeMonkeyUnity
@CodeMonkeyUnity 5 жыл бұрын
Thanks! Yeah nice that people enjoy the videos.
@mertdemirhelboyz
@mertdemirhelboyz 3 жыл бұрын
Thank god i found my answer here
@shark6334
@shark6334 3 жыл бұрын
Helps a lot! Thanks!
@JanZbedny
@JanZbedny 4 жыл бұрын
Thanks You for the helpful video, perfect tutorials! :)
@camdenjaxen3157
@camdenjaxen3157 2 жыл бұрын
i guess it's kind of off topic but do anybody know a good website to watch newly released series online?
@yamatokira8309
@yamatokira8309 5 жыл бұрын
Great tutorial . thank you
@theashbot4097
@theashbot4097 11 ай бұрын
Hey I love this tutorial I decided I was going to try to make this exact same thing my own way and then I was going to follow your tutorial, which I do with most of your tutorials. After I made mine and tallowed this tutorial I decided to take some things from this tutorial and add it to mine. the one main thing that mine has and yours does not is it will only insatiate one game object and then all of the Function timers subscribe to that one gameobject's OnUpdate function. I really like all the stuff that this tutorial has. Thank you!
@CodeMonkeyUnity
@CodeMonkeyUnity 11 ай бұрын
Yup that's a nice improvement. Normally I only use this class for some basic things so performance isn't usually a concern but yup using just a single object will indeed perform better.
@theashbot4097
@theashbot4097 Жыл бұрын
Thank you this video really helped me make a static class what makes a web call using the unity web request system (I prefer to use the HttpClient but that has no support for downloading a audio file). also did you know about the "await System.Threading.Tasks.Task.Delay(1000); // This waits 1 second"? that is what I personally use for waiting a set amount of time.
@lgmontoyauk
@lgmontoyauk 2 жыл бұрын
Many thanks for your excellent video. One questions ... At 16:47, in LIne 21 of your code, would changing "FunctionTimer" to timerName break anything or have any drawbacks? This is only just to be able to distinguish amongst FunctionTimers in the Editor hierarchy. It seems to work ... (last magic words).
@CodeMonkeyUnity
@CodeMonkeyUnity 2 жыл бұрын
That's just the name of the game object that is created so yes you can name it whatever you want.
@veteranxt4481
@veteranxt4481 Жыл бұрын
You need to swap lines 71, 72. Reason is you never remove timer from list after it was destroyed. It's destroyed before removed from list. Correct me if i was wrong. If you ever tested that static list.count it would show 2 instead of 1.
@mustafamuhammedkanmaz447
@mustafamuhammedkanmaz447 2 жыл бұрын
Hi, I am running into an issue here. I am using Unity 2021.2.7f1 and the latest version of Visual Studio Code. I am not able to test the FunctionTimer class, it does not work for any object I put it add it to (empty, main object which is a car, any other game object). I did the FunctionTimer and Testing classes exactly like you did. But when I start the game no logs can be seen in the console. I played with the time and made it 10f to see if it was becuase of loading time since I saw another person ran into that issue. How can I address this?
@durcc3459
@durcc3459 11 ай бұрын
At 15:09, I’m a bit confused at why you need to add i- - since shouldn’t the i be set to 0 when the loop is called? When i try to add more methods that pause and continue the timer, which is basically the same as the StopFirstTimerWithName() and StopAllTimersWithName() but i set the isDestroyed field to true to pause the timer and false to continue the timer instead of DestroySelf(). I found success for pause first timer with name, but pausing all timers with name freezes my unity editor. The issue somehow went away by removing i- - in the method. Although no issue had occurred yet, i am new to game development and I’m too not sure if removing i- - will cause any issues (like skipping index as you mentioned in the video). By the way, I couldn’t thank you enough with how much I’ve learned from and motivated by you (especially your c# basics playlist and 10 hr course). I really enjoy your teaching style and your videos are super helpful!
@CodeMonkeyUnity
@CodeMonkeyUnity 11 ай бұрын
The DestroySelf function removes the timer from the list which is the same list that for loop is cycling through. If you don't do i--; it will end up skipping one element. For example, there's 5 timers and you want to stop the one on index 2, when i gets to 2, it will call DestroySelf on that timer which in turn will remove it from the list which will reposition the other elements of the list and resize the list. So the timer that used to be on index 3 is now on index 2 which is the current i on the for. But then at the end of the for it calls the regular for iterator i++; so after the one that used to be on index 2 is destroyed the cycle will move onto index 3 but the timer that used to be on index 3 is now on index 2 and ends up being skipped. If you remove an element from the list you need to do i--; so you don't skip an element If you don't remove them from the list and you do i--; then yes you will end up with an infinite loop, in that example it will be permanently stuck on index 2. Thanks for the super thanks!
@asdd2a
@asdd2a 5 жыл бұрын
I'm use function timer in DoTween Asset's Sequence. it is really really comfortable
@mohnishbala9807
@mohnishbala9807 4 жыл бұрын
wait let me rephrase. so when i run the game, the coroutine works. However after I go to the menu (in the game) then get back to the level then it just freezes. Its like the coroutine was skipped and all the things after it worked
@djdavidj
@djdavidj Жыл бұрын
First, thank you for your videos. This one is great, I use it all the time and it works. My question is this … When we create initGameObject = new GameObject("FunctionTimer_InitGameObject"); This gameobject still remains in the hierarchy as a gameobject. Did I miss where you destroyed this object, or should I just leave it? I did add it to DestroySelf() UnityEngine.Object.Destroy(initGameObject); Nothing bad has happened yet. Will it be problematic later? Again thank you for your videos, I enjoy your fast paced and your teaching. Thank you D
@dadlord689
@dadlord689 5 жыл бұрын
I am using Timer component. It uses Update () if MonoBechaviour. But there is also an option to use InvokeDelayed() - it is MonoBechaviour function.
@shubhamsharma0099
@shubhamsharma0099 4 жыл бұрын
Can u help me? How to make ontriggerEnter2D waits for another ontriggerEnter2D to change scenes in unity.
@gbae3632
@gbae3632 Жыл бұрын
Hello code monkey. Thank you for the lovely video. Your videos are really helpful. I have a question at 8:35 why are you returning FunctionTimer? can functionTImer.Create function be void instead of FunctionTimer function? If it was void function instead of FunctionTimer like static void Create() instead static FunctionTimer Create(). Wouldnt they do exactly samething?
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
Just in case I want to do anything with that reference. Sometimes I want to create a timer and then cancel it afterwards or trigger it immediately, if I store the reference then it's pretty easy to do that.
@Em-ep8rz
@Em-ep8rz 3 жыл бұрын
Please forgive the beginner question, but how would you set the timer to start without any player input? As in as soon as the game starts the timer begins. Thank you!
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
Make a script and start the timer on Awake() or Start()
@christopherk4504
@christopherk4504 5 жыл бұрын
You can easier work with Invoke(action,time). That executes the action after the time. If you want to repeat this, you can use InvokeRepeat(action,time). This executes repeatly the action after time
@dandandan01
@dandandan01 5 жыл бұрын
Avoid Invoke like the plague. Too prone to errors as it's a string, and I believe it uses reflection which is also relatively slow.
@CodeMonkeyUnity
@CodeMonkeyUnity 5 жыл бұрын
Like @dandandan said, Invoke has a bunch of weird parameters and to me only makes sense when doing complex reflection stuff. Either way the goal achieved is the same, if you prefer Invoke then by all means use it, personally I find this much cleaner and easier to use. Instead of the InvokeRepeat I have the FunctionPeriodic in my Utilities which does exactly that with the same pattern as this class.
@ConchStreetStories
@ConchStreetStories 5 жыл бұрын
Is there any video on how to make factions or clans? And than choose what faction is enemy with other
@lhorbrum1818
@lhorbrum1818 5 жыл бұрын
You're too good to us.
@CodeMonkeyUnity
@CodeMonkeyUnity 5 жыл бұрын
Glad you like the videos!
@shubhamsharma0099
@shubhamsharma0099 4 жыл бұрын
Can u help me? How to make ontriggerEnter2D waits for another ontriggerEnter2D to change scenes in unity.
@DaringGamerYT
@DaringGamerYT Жыл бұрын
Code Monkey, I have this simple game idea but idk how to code in C#. I want to make a function where after a certain time, a button is unlocked to be clickable, and after clicking, the cool down time is increased by x2. Is that possible in Unity or no?
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
Sure that's possible, it's just some simple logic, there's a million ways to implement something like that, could be a simple state machine where you keep track if the button is clickable or is running the cooldown unitycodemonkey.com/video.php?v=db0KWYaWfeM
@kruth6663
@kruth6663 8 ай бұрын
Hi! At 5:40 It seems to me that, even after the timer is destroyed, that line of code: if (!isDestroyed) {...} still runs every frame, does it not? It still checks if the timer is destroyed on every frame forever, even after it's already destroyed, is that correct? If so, would it be a problem?
@CodeMonkeyUnity
@CodeMonkeyUnity 8 ай бұрын
At that point yes, but after implementing the MonoBehaviour hook that is fixed, by the end of the video the game object is destroyed when the timer elapses
@kruth6663
@kruth6663 8 ай бұрын
@@CodeMonkeyUnity Oh, I see! I haven't finished watching the video at that time, I'll watch the entire video again carefully. Thank you so much for answering!
@beta722
@beta722 3 жыл бұрын
I'm making a horror game I need to add a jumps after the Time runs out, how can I do it?
@qiangpanchen6785
@qiangpanchen6785 Жыл бұрын
Hi , I love your tutorial and it explained very well, thank you! In the meantime, when I tried to implment it in my project, I found a challenge, my method has parameters, like TakeDamage(Enemy enemy), I cannot call this method in the FunctionTimer.Create(), because it only accepts an Action without parameter... I wonder how to solve this issue ? Thank you !
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
You can encapsulate it inside an Action public void DamageEnemyAfter1Second(Enemy enemy) { FunctionTimer.Create(() => TakeDamage(enemy), 1f); } It will call that function with that enemy after 1 second.
@qiangpanchen6785
@qiangpanchen6785 Жыл бұрын
@@CodeMonkeyUnity Thank you so much, I have tried the way you described, and works charming :) Now I could get rid of the ugly StartCoroutine(s) in the middle of my code :) You are the best!!!
@liudaxun
@liudaxun 2 жыл бұрын
i have a game where projectile hits enemy and when health drops below 100, enemy dies and FunctionTimer is used to call destroy gameobject with 1 second delay but it doesn't seem to work. game kept crashing saying that gameobject does not exist when the enemy dies. I tried the coroutine method instead and it worked without any errors. Any idea why?
@CodeMonkeyUnity
@CodeMonkeyUnity 2 жыл бұрын
Maybe you're creating multiple timers? So the first one destroys the object but all the others try to destroy an object that no longer exists and breaks
@sundun1032
@sundun1032 3 жыл бұрын
Hello if something set to do after some time by functiontimer as you taught in the video think we quit the game before that time finished (if time = 50f and player quit the game at 21f ) Are there any system in your codes to save the progress of time and load it when player open the game after quit. if there hasnt a system for save progress of time After quit when player open the game timer will start from begin and player has to wait for another 50f Please tell how to add a system for save time progress . If there a system as i said Time will continue from where it has stopped ( at 21f as above example) So when player open game after quit player do not need to wait for another 50f After 29f the thing we needed to happen will be happened. Please help me
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
The timer is stored in memory so as soon as you close the game that data is gone. You need to look into how to save persistent data kzbin.info/www/bejne/bKawd3iDYtOUpqc
@sundun1032
@sundun1032 3 жыл бұрын
@@CodeMonkeyUnity thankyou very much ❤️❤️❤️
@cgcsw0237
@cgcsw0237 Жыл бұрын
Thanks ,but What is the deference from Invoke(nameof(function),3f);?
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
It also works with Lambda expressions so you don't have to create a separate function definition, that's how I normally use this class
@Xeros08
@Xeros08 5 жыл бұрын
1:30 could you explain in another video how lambdas work in C# and how they can be used? ("=>" operator)
@desstannoz
@desstannoz 5 жыл бұрын
This is function, function() { characterPos... }
@desstannoz
@desstannoz 5 жыл бұрын
U can use function() { } instead of () => blablabla..
@CodeMonkeyUnity
@CodeMonkeyUnity 5 жыл бұрын
Yeah I only noticed I did not explain that after I published the video, I should have converted the TestingAction into a lambda in the end. Hopefully soon I can do a dedicated C# course.
@tobiasceres
@tobiasceres 6 ай бұрын
Heyho! I showed it to a friend and he is not a gamedesigner but working as a programmer for 15+ years. He said two things: - It does not have a Single Responsibilty (It has the Logic and the Management of Timers in one class) - Using static methods is often "code smell". Is it true? What do you think about this?
@CodeMonkeyUnity
@CodeMonkeyUnity 6 ай бұрын
I think that sounds exactly like what a non-game-programmer would say, simply because game programming is extremely different from software development. You could definitely add those changes if you want, but I don't believe that would improve the code in any significant manner. The class is extremely functional and extremely easy to use, just call one static function and it runs a delegate. No need to worry about factories or anything like that, which usually is something you do indeed want in regular software dev but in this case would be very much overkill.
@tobiasceres
@tobiasceres 6 ай бұрын
@@CodeMonkeyUnity Thank you very much for your input!
@ingjn7858
@ingjn7858 Жыл бұрын
Great video! How can I make the action of timer to return. I use FunctionTimer inside a function and I want when the specified time passes to just return from that function.
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
Hmm not sure exactly what you mean, you can call other functions from inside that lambda. So you can do public void MyFunction() {} private void Start() { FunctionTimer.Create(() => { Debug.Log("Do something..."); MyFunction(); }, 1f); } And it will run the log and then call MyFunction();
@ingjn7858
@ingjn7858 Жыл бұрын
@Code Monkey let's say I got a function named function1, from inside function1 I use FunctionTimer to call function2 in 2 seconds. When 2 seconds passed and code is ready to call function2, let's say 70% out of 100% lines of function1 have been executed. I want the 30% remaining lines of function1 NOT to be executed, I want them to be lost. I just want to call function2 after 2 seconds and the remaining of function1 to be lost
@ingjn7858
@ingjn7858 Жыл бұрын
@Code Monkey Basically can we speak privately, Skype or whatever, of course I can pay you for a call. Send me a message if you can. Thank you
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
​@@ingjn7858 The only way to stop execution of a function in the middle is to use something like Coroutines. You can't stop a regular function once it starts executing.
@ingjn7858
@ingjn7858 Жыл бұрын
@@CodeMonkeyUnity Thanks so much for answering so fast. You really do a job that saves lives, literally! I have a function, for Networking, that receives data. How do I stop that function after 2 seconds of execution. I thought about CancellationTokens
@pengu176
@pengu176 3 жыл бұрын
So you just put the cs file in your scripts folder or your assets folder an dit works?
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
Yes
@aes0p895
@aes0p895 2 жыл бұрын
while i appreciate you including a file to do the timer for users, i'd really just like to know how it works. i'm looking through your scripts and it's pretty hard to see where the actual delay timer is coming from.
@CodeMonkeyUnity
@CodeMonkeyUnity 2 жыл бұрын
What do you mean? There's a timer at 2:55
@daniilparokonnyy7980
@daniilparokonnyy7980 5 жыл бұрын
Why you didn’t use coroutines? What are the benefits of FunctionTimer?
@iggythemad8701
@iggythemad8701 5 жыл бұрын
I also wish to know this
@peevee5588
@peevee5588 5 жыл бұрын
Stopping and keeping track of coroutines can get a little hairy. However, i use the invoke function quite abit. Usually all i need.
@CodeMonkeyUnity
@CodeMonkeyUnity 5 жыл бұрын
Coroutines force you to use a very specific pattern which personally I do not like at all. You are forced to create a GameObject with a MonoBehaviour, make a function that returns IEnumerator and has yield return WaitForSeconds(); and start the coroutine. Whereas with this class its literally just one function call and you pass in a very simple function reference and a timer. The class in the Utilities can also be used without a Game object at all. In the end they both achieve the same result so if you like coroutines you can keep using them but for me this is much much simpler.
@shubhamsharma0099
@shubhamsharma0099 4 жыл бұрын
Can u help me? How to make ontriggerEnter2D waits for another ontriggerEnter2D to change scenes in unity.
@phineasfacingforward3460
@phineasfacingforward3460 3 жыл бұрын
@@CodeMonkeyUnity I know that it's a year late, but I really appreciate you responding to our comments.
@trapshooter
@trapshooter 2 жыл бұрын
I'm having an issue, my scene takes about 4 seconds to load. For some reason those 4 seconds are taken into account on the DeltaTime, so if I have a timer that initiates on Start it will have those 4 seconds substracted without the game even starting. Anyone know a workaround for this?
@CodeMonkeyUnity
@CodeMonkeyUnity 2 жыл бұрын
You can define a simple bool firstUpdate = true; then on LateUpdate(); test if it's true and if so start the timer and set the bool to false. LateUpdate(); should only be called after the entire loading process is done and the first frame is rendered.
@trapshooter
@trapshooter 2 жыл бұрын
@@CodeMonkeyUnity yeah I tried doing this but it didn't seem to work. LateUpdate seems to be called even before the scene has loaded completely :/
@trapshooter
@trapshooter 2 жыл бұрын
I think I found the reason why this happens. It's because I'm using unscaledDeltaTime and on Start I have a bunch of things loading which drops the frames. This causes the first frame to take longer than intended. So it's just the way it is.
@tamizhankeyframe6365
@tamizhankeyframe6365 5 жыл бұрын
Some 3d game wanted 100%
@CodeMonkeyUnity
@CodeMonkeyUnity 5 жыл бұрын
Most of my tutorials work in 2D and 3D although yes in the future I would like to do some 3D focused tutorials.
@raymondreddington6521
@raymondreddington6521 Жыл бұрын
can u make in-game time-calender-season system?
@bradbrett
@bradbrett Жыл бұрын
Few tips: - Rename the class to Timer, much cleaner and easier to remember - No need to use Coroutine, just calculate the time based on Time.deltaTime
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
There are already quite a lot of Timer classes, in both regular C# and Unity, so personally I prefer a longer name to avoid naming collisions.
@bradbrett
@bradbrett Жыл бұрын
@@CodeMonkeyUnity I would create a custom namespace to avoid conflicting names, personally I think class and function names should ideally be as simple and as descriptive as possible. For Coroutine, I think calculating from Time.deltaTime is better for two reasons: - It’s faster than Coroutine - It will take into account the time scale: Time.timeScale
@devop587
@devop587 5 жыл бұрын
Can u make more like this ( ex: buy and upgrade character) enjoying :b
@CodeMonkeyUnity
@CodeMonkeyUnity 5 жыл бұрын
I am working on a RPG Leveling System video so stay tuned!
@unityvictor787
@unityvictor787 3 жыл бұрын
Thanks for Video. but I dont understand in Line:10 "private static GameObject initGameObject;" what for??? after I deleted it , my script still work fine.
@MorgurEdits
@MorgurEdits 3 жыл бұрын
Then your script works differently from his, but the line is important to store the game object for the if statement couple lines under it, also the assigning won't work if you don't have a variable to store the GameObject in.
@KriishTiwari
@KriishTiwari Жыл бұрын
Hello code monkey. I have tried to add a reminder in flappy bird for my project but i am facing some problem using System.Collections; using System.Collections.Generic; using UnityEngine; public class Reminder : MonoBehaviour { private void Start() { FunctionTimer.Create(Reminder1, 20f); FunctionTimer.Create(Close, 21f); } private void Update() { } private void Close() { Debug.Log("bye"); } private void Reminder1() { FindObjectOfType().Showcard(); } I was able to get the reminder popup at 20 sec but the bye is not getting printed. Can you please help me
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
What are you doing in ShowCard? Are you loading a different scene? If so then you're destroying the second timer. If not then it should be working. Pause the scene and look in the hierarchy, do you see two timer objects? Or maybe you're incorrectly clearing all the timers from the list instead of just the one that elapsed.
@KriishTiwari
@KriishTiwari Жыл бұрын
@@CodeMonkeyUnity showcard is basically a popup reminder in the same scene. so showcard pauses the scene and a thing pops up on the screen. so i need it to popup at 20 second. that thing is working but the debug.log is not
@sebaperales14
@sebaperales14 Ай бұрын
@@KriishTiwari Hello, well, 1 year late, but what happens may be because you pause the game (I suppose by setting Time.timeScale=0;) if you do this the timers generated in this way will not reduce the time. If you want to implement this you can change Time.deltaTime in the TimerClass to Time.unscaledDeltaTime
@nashiruddin3543
@nashiruddin3543 5 жыл бұрын
Maybe this video became more helpful if you showed as how to use this system with timeline and cine machine
@CodeMonkeyUnity
@CodeMonkeyUnity 5 жыл бұрын
I haven't used Cinemachine so I first need to do some research before I do a video on it.
@WillDev
@WillDev 5 жыл бұрын
Whats the difference with using Invoke? I find it enough
@dandandan01
@dandandan01 5 жыл бұрын
Avoid Invoke like the plague. Too prone to errors as it's a string, and I believe it uses reflection which is also relatively slow.
@WillDev
@WillDev 5 жыл бұрын
@@dandandan01 ah i see.
@CodeMonkeyUnity
@CodeMonkeyUnity 5 жыл бұрын
Like @dandandan said, Invoke has a bunch of weird parameters and to me only makes sense when doing complex reflection stuff. Either way the goal achieved is the same, if you prefer Invoke then by all means use it, personally I find this much cleaner and easier to use.
@WillDev
@WillDev 5 жыл бұрын
@@CodeMonkeyUnity ah i see.
@MorgurEdits
@MorgurEdits 3 жыл бұрын
Why you use a List instead of a Dictionary, asking out of curiosity.
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
Are you suggesting a Dictionary with the name as the key? You could do that but then timers with the same name would erase each other.
@GamerShock
@GamerShock 3 жыл бұрын
Could you use this to age npcs over time?
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
Sure, you could define a sort of Tick timer like I did here kzbin.info/www/bejne/hHfZnpmlh9OUqLs
@mohnishbala9807
@mohnishbala9807 4 жыл бұрын
Can some one please tell me how to repeat a coroutine
@amirhossien9114
@amirhossien9114 3 жыл бұрын
How to the action every 3 secends
@stefanciobotaru9678
@stefanciobotaru9678 Жыл бұрын
why not using coroutine?
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
Because personally I don't like the pattern that coroutines force you to use, you need to return IEnumerator, you need yield return and you need to call StartCoroutine(); on a MonoBehaviour. I prefer using my own class with a simple float counting down
@takrimschannel1730
@takrimschannel1730 5 жыл бұрын
Please make 2d android action game
@takrimschannel1730
@takrimschannel1730 5 жыл бұрын
@FanErii hmm bro...😀😀
@CodeMonkeyUnity
@CodeMonkeyUnity 5 жыл бұрын
Yes I would like to do some Mobile focused tutorials but not sure when.
@takrimschannel1730
@takrimschannel1730 5 жыл бұрын
@@CodeMonkeyUnity thank you very very much bro..💚💚
@Usama-kw2nd
@Usama-kw2nd 2 ай бұрын
why dnot we just use invoke funtion?
@paulblart7378
@paulblart7378 9 сағат бұрын
Invoke requires a monobehaviour to be called on, along with the string name of the function. FunctionTimer is much lighter as it can be called from anywhere and supports lambda expressions. Overall cleaner and more efficient
@blameyourm8519
@blameyourm8519 7 ай бұрын
IS THERE A SIMILAR TIMER VIDEO? NO!!! IS THERE A BETTER TIMER VIDEO? NO!!! IS THERE A BETTER DEVELOPER? NEVEEER!!!
@CodeMonkeyUnity
@CodeMonkeyUnity 7 ай бұрын
I'm glad you liked the video! This class is super useful, I've used it a ton if every single one of my games
@metawerse
@metawerse 3 жыл бұрын
FYI.... If Time.timeScale is 0, it will not work
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
The timer is based on deltaTime so yes if you set timeScale to 0 it will stop. If you want it to still work then use unscaledDeltaTime instead
@bkan1610
@bkan1610 2 жыл бұрын
InvokeRepeating();
@Bobrikaz
@Bobrikaz Жыл бұрын
doesn't work
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
What exactly doesn't work? There's not much I can do to help with that little information unitycodemonkey.com/video.php?v=KtY_5pDhqYY
@Bobrikaz
@Bobrikaz Жыл бұрын
@@CodeMonkeyUnity well it says: The names page "new Timeline does not exist in the current context,"
@federicoguillen7608
@federicoguillen7608 4 жыл бұрын
you go insanely fast. And you're not explaining most of the codes.
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
What exactly couldn't you understand?
@everydayalex4503
@everydayalex4503 Жыл бұрын
I prefer when your'e not live.
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
What do you mean? This video wasn't a livestream
@ZaCkOX900
@ZaCkOX900 4 жыл бұрын
CodeMonkey, I know you are advanced but this way of creating a timer generates trash for the garbage collector. This approach looks good on paper but is not good in general. Showing people this, they will think it's ok to make a bunch of these and later learn they should not do this. If I saw a script using this, I would have to tell them they were taught incorrectly. One timer like this is no big deal but soon as this is multiplied in an update, it becomes obvious how bad this can be.
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
Sure, you should always use the right tool for the job. If you want to create a timer on every single Update then yes this approach is not ideal. However if your goal is to simply periodically trigger some action after some time then in 90% of cases the garbage it generates will not be an issue.
@erickamini
@erickamini 6 ай бұрын
Way too fast and not enough explanations....
@CodeMonkeyUnity
@CodeMonkeyUnity 6 ай бұрын
What part couldn't you follow? If you are a beginner then yeah this is definitely not beginner level. If what's confusing you are the delegates I have a video covering them in more detail unitycodemonkey.com/video.php?v=3ZfwqWl-YI0
@outhander3941
@outhander3941 2 жыл бұрын
this video is a big ad
@CodeMonkeyUnity
@CodeMonkeyUnity 2 жыл бұрын
How so? Ad for what? It's a complete tutorial teaching you how to trigger an action after some time.
The Unity Tutorial For Complete Beginners
46:39
Game Maker's Toolkit
Рет қаралды 3,1 МЛН
I Need Your Help..
00:33
Stokes Twins
Рет қаралды 145 МЛН
Black Magic 🪄 by Petkit Pura Max #cat #cats
00:38
Sonyakisa8 TT
Рет қаралды 27 МЛН
What are Events? (C# Basics)
15:05
Code Monkey
Рет қаралды 372 М.
How to Use Actions in Unity [Unity Tutorial]
14:49
Comp-3 Interactive
Рет қаралды 30 М.
2D Movement in Unity (Tutorial)
21:09
Brackeys
Рет қаралды 3 МЛН
Unity's NEW input system in 13 minutes
13:02
BiteMe Games
Рет қаралды 10 М.
Coroutines in Unity (how & when to use them)
12:35
Game Dev Beginner
Рет қаралды 21 М.
Configurable TIMER / STOPWATCH Unity Tutorial
11:08
BMo
Рет қаралды 33 М.
Flexible LOOT SYSTEM in Unity with Random Drop Rates
13:24