I can't figure out why a game object isn't being destroyed.
I'm working on a project where some platforms have to destroy themselves upon touching some spikes at the top of the screen. The code controlling this is on the prefab for the platforms. I've tried setting the spikes as triggers, I've tried tagging the spikes, I've tried both but the platforms keep moving right on by.
private void OnTriggerEnter2D(Collision2D collision)
{
if(collision.gameObject.tag == "Platform Spikes" || collision.gameObject.tag == "Ceiling Spike")
{
GetComponent<BoxCollider2D>().enabled = false;
}
}
That's what I think is the most relevant bit but just in case I'll also post the entire code as well.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerBehaviors : MonoBehaviour
{
public int speed = 1;
public Text livesText;
int livesLeft = 3;
public Text scoreText;
int points = 0;
const int BELOW_SCREEN = -6;
// Start is called before the first frame update
void Start()
{
livesText.text = "x" + livesLeft;
scoreText.text = "Score: " + points;
}
// Update is called once per frame
void Update()
{
Movement();
Respawning();
}
void Movement()
{
float xMove = Input.GetAxis("Horizontal");
Vector3 newPosition = transform.position;
newPosition.x += xMove * Time.deltaTime * speed;
transform.position = newPosition;
}
void Respawning()
{
if(transform.position.y <= BELOW_SCREEN)
{
if(livesLeft > 0)
{
livesLeft -= 1;
livesText.text = "x" + livesLeft;
}
else
{
UnityEngine.SceneManagement.SceneManager.LoadScene(0);
}
}
}
private void OnTriggerEnter2D(Collision2D collision)
{
if(collision.gameObject.tag == "Platform Spikes" || collision.gameObject.tag == "Ceiling Spike")
{
GetComponent<BoxCollider2D>().enabled = false;
}
}
}
(Hopefully I'm not missing something painfully obvious) Thanks in advance.
Your answer
Follow this Question
Related Questions
how do i delete a trigger when it is entered? 1 Answer
Need help coding collision detection to a destroy command. 0 Answers
Unity 2D sorting item by color 0 Answers
2D: Destroy object with dynamic collider after exiting object with static collider 1 Answer
How to Destroy A GameObject when it enters any Triggers 2 Answers