How to reset the player upon collision ?
public class Player : Monobehaviour
{
//If using 3D
void OnCollisionEnter(Collision Col)
`` {
if (Col.collider.gameObject.tag == “InsertTagNameHere”)
SceneManager.LoadScene(SceneManager.GetActiveScene());
}
}
I tried this code, but it doesn’t work
Hi @iliozana2281
First assign unique tag to obstacle game object something like this “obstacle”. Add collider to this gameobject (Which you might have already done). Make sure your player or obstacle game object has rigidbody component for collision to register.
void OnCollisionEnter(Collision Col)
{
if (Col.collider.gameObject.CompareTag("obstacle"))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
Hey @iliozana2281 !
Try using this code:
public class Player : MonoBehaviour
{
void OnCollisionEnter(Collision col)
{
if(col.gameObject.CompareTag("InsertTagNameHere"))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
}
Also, make sure that:
- Both your player and the object you’re trying to collide with both have atleast one Collider component with the “Is Trigger” checkbox unchecked.
- You typed the tag name properly both in the engine and in the code.
- If you are using different layers, move them to the same layer. Or, in case you need to have them on different layers, go to Project Settings > Physics and make sure that both layers can interact between them.