NullReferenceException randomly occurs

Hello, I am pretty new to Unity and am running into something that I can’t figure out.

There are a few parts of my code that randomly causes a NullReferenceException.
They all involve using FindGameObjectsWithTag and then running a for loop through the array and doing something. For example:

landedPlatformNumber = collision.gameObject.GetComponent<PlatformController> ().platformNumber;
        GameObject[] platforms = GameObject.FindGameObjectsWithTag ("Platform");
        for (var i = 0; i < platforms.Length; i++) {
        	if (platforms *.GetComponent<PlatformController> ().platformNumber < landedPlatformNumber) {*

_ platforms .GetComponent ().DropPlatform ();_
* }*
}
What is happening is the player is jumping up off a platform onto a higher platform. When the player lands on a platform the game removes the platforms below the one you landed on. This bit of code is called when you land on a platform. The script is part of the player gameobject.
The thing is, this normally works perfectly. But every now and then, for no reason that I can figure out (even when I haven’t changed the code at all) I get the NullReferenceException. The frustrating thing is that while I try to figure out what the problem is, it will randomly fix itself and the errors no longer show up. Or I can restart unity and then it works again, even though I don’t change anything in the code!
Please let me know if there is a something that I can do to fix this - I assume there’s something obvious in my code that is incorrect or I am using something in the wrong way.
Thank you!

I imagine it’s in here:

landedPlatformNumber = collision.gameObject.GetComponent<PlatformController> ().platformNumber;
         GameObject[] platforms = GameObject.FindGameObjectsWithTag ("Platform");
             for (var i = 0; i < platforms.Length; i++) {
                 if (platforms *.GetComponent<PlatformController> ().platformNumber < landedPlatformNumber) {*

platforms .GetComponent ().DropPlatform ();
}
}
There’s a lot of improvement and null checking that should be done like so:
PlatforController pc = collision.gameObject.GetComponent ();
if (pc != null)
{
landedPlatformNumber = pc.platformNumber;
GameObject[] platforms = GameObject.FindGameObjectsWithTag (“Platform”);
for (var i = 0; i < platforms.Length; i++)
{
PlaformController pc2 = platforms .GetComponent ();
if (pc2 == null)
continue;
if (pc2.platformNumber < landedPlatformNumber)
{
pc2.DropPlatform ();
}
}
}
A few notes:
- When using GetComponent always check that the component isn’t null before accessing its members.
- Do everything you can to reduce calls to GetComponent because it is slow.
- Similarly reduce calls to Find methods because they are slow too.
- It would be better that this script is able to reference a List of PlatformControllers that have been created. That way you can check if collision.gameObject = platformControllerList*.gameObject and also use that List to iterate through without the need of all these Find and GetComponent calls. Still check for nulls though before accessing members!*

Thank you so much for this helpful response. I really appreciate you taking the time to give such constructive advice. I will take note of your points and apply them to the rest of my code as well!

Out of curiosity, is there a reason why Unity would produce this error only sometimes?