Not quite sure at what point is the collider added to the actual game object (the enemy ship)

Where exactly does the box collder get added as a component in the script?

For me if the script just stated “gameObject.AddComponent ();” I could understand how this on its own adds the collider to “this” (each enemy ship “this” script is attached too) gameObject. But how does " Collider enemyShipCollider = gameObject.AddComponent ();" add the collider to “this” gameObject. To me it seems like the collider is added to the variable “enemyShipCollider” and not the actual enemy ship gameObject.

Love the course, and the wording of the above indicates I must be learning something…lol.

I hope I don’t butcher the concepts trying to explain them as I understand them, but since it’s been nearly a full day with no reply I’ll take a stab at it:

gameObject.AddComponent();

…is operating on gameObject to AddComponent(). This happens, and the “handle” or “reference” to said object is discarded and unused. Meanwhile:

Collider collider = gameObject.AddComponent();

…Now, in addition to creating the desired component on the selected object, the handle (or reference) is passed to the variable collider of type Collider. This allows you to manipulate it directly through the collider variable (with access to the Collider class? I’m a bit fuzzy here, sorry).

1 Like

Thanks Jack. So a bit like the mathematical principal BOMDAS (multiplication before addition etc), where the component is actually linked to the gameobject first, then subsequently assigned to the variable…until someone comes up with a better explanation i’m going to go on this for now.

Privacy & Terms