I’m creating a game where speed boosts are spawned at random and I’m trying to make the game destroy them once they go off screen. The code for the Boost class is the following:
using UnityEngine;
using System.Collections;
public class Boost : MonoBehaviour {
public Player player;
void Start(){
}
// Update is called once per frame
void Update () {
Vector3 previousPosition = transform.position;
if (this.name != "Boost"){
//Move boost using the movement vector
transform.position = new Vector3 (previousPosition.x,
previousPosition.y - 0.001f,
previousPosition.z);
if(transform.position.y <= -10){
Destroy(this, 1);
}
}
}
}
In the editor, once boosts get to the desired position, they stop, but they remain there and still appear on the hierarchy. I’m completely lost at why this happens. BTW I’ve also tried using DestroyImmediate(), same results.
PS: Before adding this:
if (this.name != "Boost"){
The prefab would stop moving upon reaching the destroy position, and no more clones could be made, so the game detected that the object had been destroyed, buy the prefab was still displaying on the editor and hierarchy.