I am trying to write a kill player script but it is not resetting my scene can someone tell me why?
using UnityEngine;
using System;
using UnityEngine.SceneManagement;
public class killplayer : MonoBehaviour
{
public GameObject player;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.CompareTag("Player"))
{
Scene currentScene = SceneManager.GetActiveScene();
SceneManager.LoadScene(currentScene.name);
}
}
}
Have you debugged that the OnCollissionEnter2D is being called at all? Or if the CompareTag is passing? If not, I would investigate why its not.
Sounds like you wrote a bug…
And that mean it is…
Time to start debugging!!! Here is how you can begin your exciting new debugging adventures!
Debugging can allow you to get all the information you need to discover what the problem is. Once you actually know the problem, only then will you be able to create a solution…
The most coming issue that happens is one of the following:
- the code you think is running isn’t
- the code is running sooner or later than you think
- the code is running less or more often than you think
- the code is running somewhere else than you think it is
- you haven’t look at the previous errors or warnings in the console
To help gain more insight into your problem, I recommend literally sprinkling Debug.Log() statements everywhere near the source of the problem to display all the information in realtime.
“When in doubt, Debug.log it out!"
It seems like collision is not triggering at all. Any tips to fix this?
So apparently by turning off “is trigger” in the Box Collider 2d it works now so now I just have to figure out how that made it work.