Now that we have our player shooting lasers and our enemies spawning; as shown below; how do we go about making the colliders on these objects interact?
When developing a program, variables are stored in the memory at different locations. If an object could see any other object members, there would be a risk to modify them even though it was not intended. There are various way to access variables, one of those ways is by using GetComponent.
In order to use GetComponent, we need to be able to identify which object the component we are after is on. An easy way to do that is through the use of something called Tags.
In order to access ; let’s say the script Player attached to our Player object; we first need to create a reference to that script. This is where GetComponent comes in.
Now that we have a reference to the component we are after, we can access any public method within that script.
In order to avoid possible null reference exceptions when running our game, it is best practice to check that the component we are after exists. We can do this by performing a check using; in this example; if(player != null), before trying to use a method within it.