Need help-how to destroy this gameobject

Hi guys, i got a very very strange problem, i create a sphere gameobject,and i can delete it directly, but why does’t work if i add a if statement
//This is work correctly
targets = GameObject.CreatePrimitive(PrimitiveType.Sphere);
targets.transform.position = new Vector3(0, 10, 0);
targets.transform.localScale = new Vector3(Random.Range(1.0f, 5.0f), Random.Range(1.0f, 5.0f), Random.Range(1.0f, 5.0f));
targets.AddComponent(“Rigidbody”);
//add force and direction for the sphere
targets.rigidbody.AddForce(Vector3.down * speed * 1);
destroy(targets,2);

//this not
//The targets
targets = GameObject.CreatePrimitive(PrimitiveType.Sphere);
targets.transform.position = new Vector3(0, 10, 0);
targets.transform.localScale = new Vector3(Random.Range(1.0f, 5.0f), Random.Range(1.0f, 5.0f), Random.Range(1.0f, 5.0f));
targets.AddComponent(“Rigidbody”);
//add force and direction for the sphere
targets.rigidbody.AddForce(Vector3.down * speed * 1);

if(targets.transform.position.y <-5)
destroy(targets,2);

i tried so many different ways, but still not work,really need help. Much thanks for this!!:stuck_out_tongue:

in which function is your if statement ?

That would be simple… target.transform.position.y is never less than -5…

Awake()

ok,i try to change the value

i think the targets are created at the point(0,10,0),so that’s why i can’t delete it if it’s transform.position<-5. Is anybody can tell me how to get the target’s point, because the targets has a rigidbody,so it always going down,the position.y always change.

Awake is called once everytime the object is…well woken up… either created, re-enabled…whatever. so your objects y value is only set that one time when awake is called… and it obviously isn’t less then -5 as BigMisterB has said.

You want to check your objects position all the time or when it moves for it to work so use a repeating function like update, or invokerepeating your own function to check position.

ok,i think i understand sth. Thx so much,make lots of sence for me