De'Shaun De'Shaun - 11 months ago 42
C# Question

How would i let the c# script know of a collision?

I have this script for this game I'm making and I'm trying to make the object that a bullet hits dissapear.. Heres my script:

void onCollisionEnter()
{
if (collision.gameObject.tag == "Enemy")
{
Destroy(collision.gameobject);
Destroy(gameObject);
}
}


The error says




Severity Code Description Project File Line Suppression State
Error CS0103 The name 'collision' does not exist in the current context First person game.CSharp C:\Users\desha\Documents\First person game\Assets\Prefabs\Bullet_kill.cs 20 Active




It says this error twice, So if collision doesn't exist in this context, How do i get whatever the bullet collides with to disappear?

Please help.

Answer Source

It seems to me you are missing the function input. change this line

void onCollisionEnter()

for this one here:

void OnCollisionEnter(Collision collision)

I hope I was of help. :)

The error appears twice because you are calling "collision" on the if statement, and then again on Destroy(collision.gameobject), which by the way should be Destroy(collision.gameObject).