I recently discovered the Event system and I have some questions about its performance.
I have a point and click game with a cursor that will be controlled by a controller.
In a scene there will be, let’s just say 60 object with 3 different script:
20 object with ScriptA, 20 object with ScriptB, 20 object with ScriptC,
All 3 scripts are gonna do different things if I point at it with my cursor (using raycast) and I press the X button.
If I’m using event with transform parameter (to check if the cursor is pointing at it) when I press the X button which one of this options will the event do?
a. It will only run 1 script (the one the cursor is pointing at)
or b. It will try to run all 60 of them, but all of them failed except the one that currently being pointed at by the cursor.
I have no problem if the answer is A, but if the answer is B will it impact my game performance?
If you are raycasting against an object, only the object with that script will be affected then – you can adjust this in code. So I think the answer is A. When you start the game, each object has an “instance” of a script, which basically means they are separate scripts of the same script (essentially).
Even if they are not, you should be able to to tag, or set a boolean on each object that will keep all scripts from running anyways, so that is a second option to keep all scripts from running (even if what I said first is incorrect for some reason).
Be careful though with the event system, because the event system itself is very slow if there are too many in the game. For example, if you have 20 World Canvases in your game, all with event systems, that will slow down your game dramatically.
I think you should only need one event system. I would not put 60 event systems in the game all running at the same time, that will affect performance very badly.
Also if you are using Unity’s default Event system script – it is not modifiable. You cannot change the code in it yourself, which is a big drawback if you want to do that.
with 4 object it
print 4 “Called”
and 1 “ActuallyCalled”
does calling the function without actually doing the action affect performance? especially with more object, hypothetically a lot (mine is only around 10, but let’s just say if there is a lot more)
if it slow… will it be slower than using update on each object?
since all the function gonna get called, but the action only executed on 1 object