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)
GetComponent<Collider>().transform.CompareTag("Player")
//Instead, you should be using
col.gameObject.CompareTag("Player")
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;
@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.
It’s OnCollisionEnter, not onCollisionEnter. Unity event functions are case-sensitive.
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.
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.