Couple questions regarding overall code

Hello! As I was going through the lecture and reviewing my code I realized there is actually a lot I don’t know so I figured this would be the time to ask.

  1. I see that Rick decided to use the Update() method for WeaponZoom.cs . Why is that? At a first glance I would have thought that because the method is “triggered” by a single button (right mouse key) with no requirement for adjusting transforms or any complicated movement patterns, it seemed like it could have been handled some other way that doesn’t involve the use of Update. (I also solved it successfully using Update method initially, but I was wondering if there were better ways.)

  2. The reason I thought of question 1) was because I was worried how much processing power it would take if we used Update() method for everything we could find. Are there golden rules for what type of code we SHOULDN’T put in Update due to CPU constraints? Is there a reference for me to intuitively know “Hey, this probably costs a lot of processing power so I shouldn’t put this code here / write code in a different way”?

  3. While I was thinking on how to ask question 1), I was sifting through my code and realized I didn’t understand how ReloadGame() in SceneLoader.cs and AttackHitEvent() in EnemyAttack.cs were “triggered”. After trying to track down how these methods were “activated” I remembered that the “trigger” for AttackHitEvent() was in the Animation area of Unity, and for ReloadGame() it was in the UI’s buttons. This got me thinking: If this was a larger scale game, it would be difficult to find/remember some of these things over time because it’s not explicitly written in code. This also made me think FindObjectOfType/GetComponent is generally superior to [SerializeField] because things are written in code and it seems easier to track. Here are my questions for 3):

  • Are there easier ways to track down “triggers” that are specifically in Unity program (not written in code) easier or do I have to manually sift through it (as in, check all my animations for the “trigger” of an enemy or go through all the buttons to see which “triggered” my method)?
  • Is GetComponent or using Tags just a better method/practice overall vs FindObjectOfType (because of high CPU cost) and [SerializeField] (because it’s difficult to always know what is connected to which)? I understand that GetComponent has its restrictions of not being able to find the object if it’s outside the parent, but was wondering if I should cater to GetComponent from now on by how I attach objects in the Hierarchy.
  • Is there a proper vocabulary to describe “triggered” and “activated”?

We need to know when this single button is pressed. The new input system can trigger events, but with the old one we need to continuously check if the button was pressed. So, we do this in the Update() method

Not really. If it needs to happen all the time, it goes into Update(). Well, almost. Normal ‘all the time’ stuff goes into Update(). Physics updates (like setting Rigidbody.velocity) should go into FixedUpdate. And we usually put camera stuff into LateUpdate(). LateUpdate() is the last update that will run before Unity does what it needs to do to complete the frame. We like camera stuff here because it means that all the things that need to move have finished doing so for this frame, so there will be no weird things on the camera.

Unfortunately not - as far as I know. For animation events, it’s a good idea to put a comment with the function stating that it will be called by an animation (perhaps add the animation name). I suppose you can do the same with button events. You could also name the methods appropriately to help. Like ReloadGameClickHandler() as opposed to just ReloadGame(). From Unity 2022 the built-in search functionality (Ctrl+K) has greatly improved, but I still don’t think it will solve the animation trigger problem. Perhaps it will.

[SerializeField] is the best option in terms of performance (when you can use it but that’s not always the case). With it you will have whatever it’s linked to in the inspector and just clicking on it will ‘ping’ the actual instance. Whatever is referenced by it will immediately be available, while all the other options require Unity to search for whatever it is you want.

Yes. We say “triggered” and “activated”. :grin:
“triggered” would probably be used in terms of events. I sometimes talk about methods being “called” and events being “fired”. Exceptions being “raised” or “thrown”. I think a lot of terminology comes from your background. Some languages “raise” events while others (like C#) “throw” them. What I mean by that is that if you want to “throw” an exception yourself in C#, you would quite literally (almost) do that

throw new NullReferenceException();

In python, you would raise it instead

2 Likes

Thank you for your detailed answer!

The types of updates was something that I put on the backburner as I was initially intimidated by the usages, but seeing you explain it makes it really simple to understand.

I completely forgot about the usage of comments – surely it’s useful for cases like these. And I’ve seen so many videos that naming methods properly are extremely important, now I see why.

And it’s good to know that SerializeField is the least CPU intensive – I’ll definitely try to incorporate it more into my practices/projects.

Thank you as always.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms