I am currently working on a 2d top down game. basically when the player touches the enemy, The scene changes to the main menu. what i have so far is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Death : MonoBehaviour
{
public bool isDead;
void Start()
{
isDead = false;
}
void update()
{
if(collision.gameObject.CompareTag("enemy"))
{
isDead = true;
}
if(isDead = true)
{
SceneManager.LoadScene("MainMenu");
}
}
}
enter code here`
I’m not really sure what the question is, but checking your code maybe you should consider explore OnCollisionEnter2D, instead your code within Update function that runs each frame (Unity - Scripting API: MonoBehaviour.OnCollisionEnter2D(Collision2D))
I’m not sure whether you’re using isDead for something else, but since you change it to true and right away switching to different scene, it seems unlikely
anyway, what you’ve described, I would do with 2D colliders (don’t forget to attach also rigidbody, otherwise collider will be assumed static and if you are moving it, it will kill your performance)
and within code then something like
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.CompareTag("Enemy"))
{
SceneManager.LoadScene("MainMenu");
}
}
Yeah ,you need colider2d and rb2d on player and enemy
Would that help the fact that i die as soon as i start the game?