Respawn after Collision

Hey everyone, I have already searched and tried various answers on here as well as tried various online tutorials and nothing is working. So, I am trying to make my player respawn when they fall of my level. I have created an empty gameobject and placed it beneath my level, as well as an empty gameobject that acts as the respawn point. My player has a rigidbody and all of that good stuff but when they collide with the KillBox (as I have it called in my game), they simply stand on it and nothing happens. Any help would be greatly appreciated as this is a project that is due soon!!
(Here is my code)

public class Respawn : MonoBehaviour {

[SerializeField] Transform respawnPoint;

void onCollisionEnter (Collision col) {
	if (GetComponent<Collider>().transform.CompareTag ("Player"))
		GetComponent<Collider>().transform.position = respawnPoint.position;


The error is that you are using

//Instead, you should be using

When you use GetComponent collider you are getting your own collider, then you tell it to compare your own tag with “Player” which will never match because (I’m assuming) this script is attached to the respawn point and the respawn point does not have that tag. Furthermore, when you change the position you won’t be moving the player. Again you would be moving whatever object this script is attached to since, calling GetComponent by itself will only return components attached to the same game object, the calling script is attached to.

So, instead of doing

GetComponent<Collider>().transform.position = respawnPoint.position;
//You should be doing
col.gameObject.transform.position = respawnPoint.position;

Hope this helps!

@MacDx I made the changes you suggested and still nothing happened. The code is attached to the killbox gameobject itself, which is an empty game object with a box collider underneath my entire level, not the respawn point.

I just did that and it worked facepalm X 10,000 thanks for helping me realize my stupidity XD

Your code should be like this:

void OnCollisionEnter(Collision col)
		col.collider.GetComponent<Rigidbody>().position = respawnPoint.position;

There are three problems I have noticed:

  1. It’s OnCollisionEnter, not onCollisionEnter. Unity event functions are case-sensitive.

  2. You should access col parameter of OnCollisionEnter method instead of GetComponent. GetComponent will return the script holder’s own component which you may not want.

  3. Your player uses Rigidbody but you’re using transform.position to move it. That’ll be costly, as the engine needs to recalculate the position relative to the Rigidbody before moving. Instead, if the player’s rigidbody isn’t kinematic, you may want to use Rigidbody’s position instead.