- Home /
Establish this as a gameObject to destroy
I have a series of game objects that are all the same prefab, same tag, and their only distinguishing feature is a public string called "label". They all share a controller script in C#.
When my player executes an action, I want the game object its colliding with currently to disappear and trigger a series of events.
How can I use the controller script to establish the game object currently being collided with to ultimately use Destroy() upon?
Answer by fafase · Jun 25, 2014 at 06:02 AM
So you want to check if the colliding object contains the script to establish it as a candidate for destruction. In simple words, if the guy has the script, destroy it.
void OnCollisionEnter(Collision col){
if(col.gameObject.GetComponent<Controller>() != null){
Destroy(col.gameObject);
}
}
in UnityScript:
function OnCollisionEnter(col:Collision){
if(col.gameObject.GetComponent(Controller) != null){
Destroy(col.gameObject);
}
}
Your answer
Follow this Question
Related Questions
Is there a 3d person controller menu? Others have it, but I don't 1 Answer
MMD How to export model and animations to Unity as 3rd person controller? 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
OnTriggerEnter - destroy (this.gameobject) if it collides with anything 2 Answers