Why not protect transform.position in Projectile.cs?

Can someone please explain the thinking around why we need to protect guardPosition = transform.position in AIController.cs, yet @sampattuzzi decided (unfortunately without much explanation) that we didn’t need to protect transform.rotation (via LookAt()) in Projectile.cs.

It seems we’re protecting guardPosition “to be safe” even though it isn’t currently accessed by another component’s Start method. If that’s the case, shouldn’t we be proactive and try to protect a Projectile’s transform.rotation too?

Or does @sampattuzzi not care in this case because we don’t expect a Scene asset to ever contain a Projectile instance at Load time?

Just wondering if there’s some reason or explanation around this omission that might help me form my mental set of guidelines.

As I’m reading the relevant code, in AIController, we’re protecting PatrolPath, (in effect defaulting to guardPosition), but not actually protecting the guardPosition = transform.position call.

Within a MonoBehavior, you can be assured that the behaviour’s transform position exists. There’s no need to null-check your own transform. You do, however, need to null-check external references before accessing their transforms. So… in Projectile, we check to see if Health target is null… if it’s null, we do nothing, if it’s not null, then we know implicitly that we can access target.transform.position.

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

Privacy & Terms