- Home /
How to remove script on collision?
{
void OnCollisionEnter (Collision col)
{
if(col.gameObject.name == "DeathCube1")
{
col.gameObject.RemoveComponent<Ctrl>();
}
}
}
Im trying to remove Ctrl from Deathcube1
Answer by christuart · Aug 24, 2015 at 02:42 PM
The Destroy() method can be used to remove components or destroy GameObjects, depending what you pass it.
In your case:
Destroy(col.gameObject.GetComponent<Ctrl>());
As an aside, if you are trying to use Destroy in Gizmos code, you have to use DestroyImmediate() or Unity will simply warn you that destroying objects or components outside of play mode is dangerous - which is certainly true.
Your answer
Follow this Question
Related Questions
Can't change variables on collision, if statement being ignored 1 Answer
If statement null check not working as expected 1 Answer
Increment based 'if' conditions not being met for the later conditions. Any solutions ? 0 Answers
Odd collision behaviour with If/while statement 1 Answer
Can't access nested if... 2 Answers