I am not very good with colliders and I am having a lot of trouble with it. What I want to get my script to do is when the player collides with any enemy object it will be destroyed along with the enemy object. The player will lose a life and then the player will respawn again until lives = 0. Could someone help with this? I am using the newest version of Unity 2D.
using UnityEngine;
using System.Collections;
public class PlayerLives : MonoBehaviour {
public int life = 3;
public GameObject spaceShip;
private Vector3 currentposition = GameObject.Find(“spaceShip”).transform.position;
void Start()
{
}
void Update()
{
}
void OnTriggerEnter2D (Collider2D other){
if (GetComponent<PolygonCollider2D>().gameObject.tag == "Enemy") {
Destroy(other.gameObject);
life = life - 1;
if (life == 0) {
Destroy (this);
}
if (life > 0) {
Destroy (this);
Instantiate(spaceShip, currentposition , Quaternion.identity);
}
}
}
}