r/unity • • 14d ago

Doubt about MonoBehaviour

So i need a timer for multiple components, my first tough was make it extend MonoBehaviour so i can drag it to another script (which is very required) but technically is somthing that other scripts will be using, not the game object directly, so i think it needs to be a raw class? with the [System.Serializable] attribute and have a variable of it when is required, but with this method i cant drag the class to a variable.
What is the usual in this cases? i can't find out

5 Upvotes

8 comments sorted by

3

u/wallstop-dev 14d ago

Can you clearly explain the precise nature of your use case? What exactly are you timing (unity time? real time? game time? Your own concept of time? Something else?) And, how/why does it integrate with the components?

Without a clear use case, it is difficult to provide advice.

1

u/xXx_ask-master_xXx 14d ago

in my game a lot of things require time, one example could be this class called "ExistenceChecker" which if after x amount of time is not notified by the player (isn't important how for the case) the player loses the level, other things can be just the time for some behaviour like a engine propulsing for 1 second, so i think make this a common class to not re-write code

2

u/wallstop-dev 14d ago

There are many ways to approach this.

One of them is to write a utility function that takes in a start time and a duration and returns true if the duration has passed. How much you want to typify it (`float` v `TimeSpan` v `YourOwnCustomThing` is up to you)

One is to write some centralized "service" that all of your code pushes a concept of "Fire this callback when this duration has elapsed", which could be a monobehavior or driven by a monobehavior. This thing would be responsible for polling/checking expirations and firing callbacks.

The list goes on.

Just pick one, whatever works, it doesn't really matter. If it gets too annoying to maintain for your specific use cases, you'll better understand what the problems are and what a good solution for you would look like.

In my game projects I heavily decouple things with events (the concept, not "UnityEvent") and, if I ever need elapsed checking, do the "record a start time and check at an interval to see if it's past a duration" thing.

2

u/Mechabit_Studios 14d ago

probably better off using events and have other classes register as listeners so you aren't as tightly coupled

1

u/Informal_Engineer794 14d ago

If i where you i would make a struct with the timer data and make a list to use as a manager, maybe add a unityevent field if i want to pass from the inspector the function to call on timer end, so on update you loop that list, call tick on each entry, inside tick you launch the event when timer reach 0, add a bool to delete the entry if you want to reuse it. Use a singleton too so you can add entry on code easily.

No idea if this will help you

1

u/MeishinTale 14d ago edited 14d ago

You have 2 routes usually; Decoupled; Each script calls its own checks using its own implem (it's not even worth to make a static helper since it's usually only 5-6 lines of codes and you need to handle the scope of the routine canceller).

Centralized; A scheduler. Usually a monobehavior called using either a singleton pattern or a service provider.

In both case if timer is sparse you can implement the timer check with coroutine/unitask, decoupled is privileged for scaling/keeping everything tidy. Doesn't require monobehavior/ update at all.

If you have a lot of timers which finishes every frame you'd usually prefer doing that in an update method, hence a service/singleton scheduler is privileged in that case.

1

u/Competitive-Elk763 14d ago

Personally i use a singleton manager that handles timed events (as well as Update calls).

It would be something like: GameManager.Instance.AddTimeEvent(duration, callback if interrupted, callback if completed)

Instance being a public static field. The gameManager being a Monobehavior.

1

u/ledniv 13d ago

I would not make the timer a MonoBehaviour just so it can use Update().

A timer is really just data. Something like remaining time, duration, and whether it is active. The script that needs the timer can hold that data, and your game loop can pass deltaTime into a simple function that updates it. If you eventually have lots of timers, you can keep all of that timer data together and update it in one place instead of having lots of MonoBehaviours each running their own Update().

If you mark a normal class or struct as System.Serializable, Unity can show its fields inline in the Inspector, but you cannot drag it into a field because it is not a Unity object. If you specifically need a reusable asset that you can drag around in the Inspector, that is where a ScriptableObject can make sense. But I would use that for timer configuration, not for the actual runtime countdown state.

In general I try to use MonoBehaviour when I actually need something from Unity, such as a GameObject, Transform, input, physics, or the Unity lifecycle. Gameplay data and logic do not need to become MonoBehaviours just because the game is made in Unity.

Small plug: this is a big part of High Performance Unity Game Development with Data Oriented Design. The architecture uses one central Unity game loop and keeps most gameplay state as data and most gameplay logic as normal functions instead of spreading it across lots of MonoBehaviours.

https://www.manning.com/books/high-performance-unity-game-development