Help with Collider?
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);
}
}
}
}
Answer by Zoogyburger · Mar 18, 2016 at 10:08 PM
It is best to say what exactly your error is on if anything is happening at all. Replace line 15 with
if (other.gameObject.tag == "Enemy")
Answer by Xanthexxx · Mar 19, 2016 at 08:18 PM
Thank you that worked. Just need to fix some minor errors now.
Your answer
Follow this Question
Related Questions
How To how ?? 2 Answers
Collision with object not working. 2 Answers
Wheel rotation 1 Answer
OnTriggerEnter Issue - Collider problem 0 Answers
Collision With Text 0 Answers