CompareTag being ignored?

So, I have a small 2D platformer I’m working on. In the game there are platforms (of course), and colliders in specific positions under them. When the player falls off the platform, I want them to teleport to a specified respawn node and add 1 to the number of times they’ve fallen (a UI element).

It seems like the code below is completely ignoring the CompareTag command, as each of the colliders are tagged uniquely. It’s just recognizing that I’ve hit something and respawning me. I fixed an issue where it would add 3 instead of 1 every time I died, but now it spawns me at the 1st respawn point regardless, even when I should be at the 2nd or 3rd.

This is the code I’m using, written in C#. The two sections commented out are the ones in question. It’s been a couple months since I worked on the game. Has any of the code been deprecated? I greatly appreciate any help.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Spawning : MonoBehaviour {

public AudioSource source;
public AudioClip deathSound;

public bool dead = false;

public Transform spawnPoint;
public Transform respawnPoint;
public Transform respawnPoint2;
public Transform respawnPoint3;
public Text Falls;

public int timesFallen;

// Use this for initialization
void Start () {
    transform.position = spawnPoint.position;
    timesFallen = 0;
    Falls.text = "Falls:";
}

void OnTriggerEnter2D(Collider2D other) //When I hit a "deadzone", or the colliders below
{
    if (other.gameObject.CompareTag ("Respawn")) //if it's the 1st collider, take me to respawn point 1
        respawn();
        transform.position = respawnPoint.position;
        
        
    /*if (other.gameObject.CompareTag ("Respawn2")) //the same thing but for spawn point 2
        respawn();
        transform.position = respawnPoint2.position;
        
        
    if (other.gameObject.CompareTag ("Respawn3")) // same but for 3
        respawn();
        transform.position = respawnPoint2.position;*/
           
}

void SetTimesFallen()
{
    timesFallen = timesFallen + 1;
    Falls.text = "Falls: " + timesFallen.ToString();
}

void respawn ()
{
    dead = true;
    source.PlayOneShot(deathSound);
    SetTimesFallen();
    dead = false;
}

}

I’ll have to admit that I cringe massively every time someone tries to use Tags this way. Tags are useful for common groups of objects. You can only create a finite number tags, so using tags to identify a potentially infinite number of objects is not getting you very far.

You can see a very related issue in the if statements you have commented out. You have three times pretty much the same code, only slightly different. If you have 20 respawns, you will have 20 of those if statements and 20 variables cluttering up your code.

The best solution to avoid this kind of code depends on the situation. In your case, you seem to have a set of objects that each refer to one specific object of another set. Namely, each of the respawn triggers the player can touch implies one specific respawn point.

So I’d recommend to write a very small script:

public class ReapwnTrigger : MonoBehaviour
{
  public Transform respawnPoint;
}

that goes on each of your respawn triggers. You can then drag the respawn point corresponding to the platform into the “Respawn Point” property.

The player script, in the meantime, does this:

void OnTriggerEnter2D(Collider2D other)
{
  var respawnTrigger = other.GetComponent<RespawnTrigger>();
  // Since the trigger we entered might be something other than a RespawnTrigger,
  // we need to check if it has the component first
  if(respawnTrigger)
  {
    // Okay, it is a RespawnTrigger, so lets teleport to the respawn point it wants us to go to
    transform.position = respawnTrigger.respawnPoint;
  }

And that’s that.

No more tags, no more potentially infinite if statements, no more the player knowing the whole level before even going into it.