I’m trying to make the asteroid destroy itself after the player gets too far
{
public Transform transform;
public Rigidbody2D rb;
public Vector2 randomVector;
float rotationForce;
GameObject Player;
private Transform playerTransform;
GameObject Asteroid_PreFab;
GameObject LogicMaster;
private logic_Master logicMaster;
void Awake()
{
Player = GameObject.Find("Player");
Asteroid_PreFab = GameObject.Find("Asteroid_PreFab");
LogicMaster = GameObject.Find("LogicMaster");
playerTransform = Player.GetComponent<Transform>();
logicMaster = LogicMaster.GetComponent <logic_Master> ();
}
// Use this for initialization
void Start () {
Vector2 randomVector = new Vector2(Random.Range(-3,3), Random.Range(-3,3));
Debug.Log(randomVector + "" + randomVector.magnitude);
rotationForce = Random.Range(-1, 1);
float scaleFactor = Random.Range(0.5f, 2f);
Vector2 scaleVector = new Vector2(scaleFactor, scaleFactor);
transform.localScale = scaleVector;
rb.velocity = rb.velocity + randomVector;
rb.AddTorque(rotationForce, ForceMode2D.Impulse);
}
// Update is called once per frame
void Update()
{
Vector2 playerDistance = new Vector2(transform.position.x - playerTransform.position.x, transform.position.y - playerTransform.position.y);
if (playerDistance.magnitude > 30 )
{
Debug.Log("true");
Destroy(Asteroid_PreFab);
}
}
}
At the very end when the magnitude exceeds 30, its outputting true as per debug.log, but is not destroying the asteroid.