Procedural Obstacle Cleanup
Hi all,
I have the script "ObstacleDelete" attached to a prefab of Obstacle, with all the relevant connections on the prefab - script is as follows:
using UnityEngine;
public class ObstacleDelete : MonoBehaviour
{
public Transform player;
public float destroyDistance = 5f;
void Update()
{
if(player.position.z >= gameObject.transform.position.z - destroyDistance)
{
Debug.Log("we passed an object with the player");
Destroy(gameObject);
}
}
}
Basically, when the player moves past the obstacle, it should get destroyed when its "destroyDistance" behind the player. Shown below is a Graphical representation
Red Square being the player, grey block being the obstacle.
This however doesn't happen and my debug statement doesn't even get registered and i cant for the life of me figure out why...
Answer by Darksorcerer57 · Aug 03, 2017 at 10:59 PM
Sorry if this is annoying but here are a few questions: Did you change this distance in the inspector? Have you tried adding the destroyDistance instead of subtracting it? Have you tried checking for a less then instead of a greater than? (The last two depend on your game setup)
Your answer