I’m having some trouble understanding the fundamentals.
Should only one script have an update or fixed update function? How many files should have it?
What types of scripts should have it?
Cheers!
I’m having some trouble understanding the fundamentals.
Should only one script have an update or fixed update function? How many files should have it?
What types of scripts should have it?
Cheers!
A monobehaviour script can have any number of the Unity magic methods: Unity - Scripting API: MonoBehaviour
So it can have both an Update call, FixedUpdate call, even a LateUpdate call.
You can only have one MonoBehaviour or Scriptable Object class per script file, and the class name needs to match the file name.
For other types of C# objects (regular C# classes, static classes, enums, etc), it doesn’t matter.
Important to remember that ‘scripts’ are really just a text file. It’s the C# code we write in them that actually does anything.
If you mean this in terms of optimization, you should have as few as possible, however there is no limit nor official recommendation, you are free to have as many as you’d like. And games are perfectly fine if they have couple of dozens statically updated objects.
However if you intend to have objects dynamically pop in and out, consider that there is some list that has to be maintained and if this list is changing constantly or grows beyond 50-60 objects, this is a potentially hefty workload that has to be maintained per frame.
Obviously this system lives on the C++ side of the engine, where things are very speedy and designed to be abused, but when we’re talking about 100+ Hz of refresh rate, these things add up.
So again, if you’re a beginner, don’t fret about it. However, if you intend to make every bullet actively run it’s own update (with no pooling and such techniques), try to rethink your approach or your game won’t scale gracefully.
Think in terms of modularity. I have an in-game flight system for instance. It has an Update method call in one of the classes because the flight system needs it. The logic could be placed within some other class’s Update method but it would be an odd requirement that some arbitrary other class handle flight logic. Particularly as all other flight-related properties and methods would be in flight-related classes.
Similarly if a video is playing multiplayer sync needs to occur in an Update method and again placing it within the video class makes the most sense.