- Home /
Why Does it destroy all gamObjects with the same script
var c : crossbow; var player : GameObject; var item : GameObject;
function Update () { var dist = Vector3.Distance(playerD.transform.position, item.transform.position);
if(dist <= 10 && Input.GetKeyDown("p")) { c.arAmount += 5; Destroy(gameObject); }
}
it destroys all the objects with the same script but i want it to only destroy the item the player is close to but it won't work it destroys every GameObject with the same script when it should only destroy itself, it is connected to the item object not the player please help
Answer by whydoidoit · Nov 23, 2012 at 12:05 PM
Your problem is that you are check item.transform.position from the player - but then destroying the current game object. I would imagine you want to test how far away the item with the script on it is?
That would be
var dist = (playerD.transform.position, transform.position).magnitude;
I'm not sure what item is....
Answer by SARWAN · Nov 23, 2012 at 12:01 PM
you just mention
Destroy(this.gameObject);
(or)
Give name of the GameObject That you want to destroy
Destroy(item);
It will only destroy the particular gameObject
Your answer
Follow this Question
Related Questions
Instantiate prefabs before it comes into view 0 Answers
Wy does this destroy script not work? 2 Answers
Destroy specific child object 1 Answer
Gameobject is not being destroyed 1 Answer
Check if is destroyed 0 Answers