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.
-
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.)
-
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”?
-
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”?