my script says OnCollisionEnter() error bce005 and it wont work

ok so im new to unity and im making an fps, this is the script i’ve made

var health = 10;
var something : GameObject;

function Update ()
{

if (OnCollisionEnter(something)) {
var health = -1;
 }
}

i want to attach this to an enemy so when they collide with a bullet they will lose 1 health (i tried to put bullet instead of GameObject but it came up with an error)

when i try to build the game it says BCE0005: Unknown identifier: ‘OnCollisionEnter’
someone help plz

What does "not working" mean? It certainly compiles without errors as long as rbody is a Rigidbody. This line should only remove the Y position constraint. I just tried it in my test project and it work as expected. An object with rigidbody and all 3 position constraints activated. The object just floats in the air. When i execute your exact line on that rigidbody it starts falling down and only the Y position contstraint was removed while x and z are still enabled. If you need further help with one of your issues, post a seperate question and add more details.

3 Answers

3

OnCollisionEnter doesnt work that way, its its own function. so instead of being within update, it would look something like this:

function OnCollisionEnter (collision : Collision) {
if(collision.gameObject == something){
health -= 1;
}
}

Then obviously make sure ‘something’ and ‘health’ are defined correctly.

Note: Take syntax with a grain of salt, I don’t normally code in java.

now it says BCE0043: Unexpected token: collision.

That's me not knowing java syntax. Itd be (collision : Collision) like unluckyBastard said. Not sure why the if isnt working, could you post your most up to date code?

function OnCollisionEnter(c : Collision){
//code here
}

thanks that fixed that bit but now it says unexpected token if -_-

it looks like this now var health = 5; var something : GameObject; function OnCollisionEnter(c : Collision) if (collision.gameObject == something); { health : 1};

well... the if error is till there and its telling me to put the semicolon back -.-

var health = 5;

function OnCollisionEnter(collision : Collision) { 
   if (collision.gameObject.name == "Bullet") //change Bullet to whatever you bullet is called, pay attention to caps.
   {
     health--;
   }
}

still have the error -_- i cant see why it doesnt just read it