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;
}
}