- Home /
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
Answer by djmorrsee · Sep 05, 2011 at 03:23 AM
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.
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?
its not java, its javascript. And its not really javascript, its unityscript which is javascript-like syntax but using .net/mono environment.
Answer by unluckyBastard · Sep 05, 2011 at 03:37 AM
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};
you have to take out the semicolon after the if statement if(collision.gameObject == something){ health -= 1; }
well... the if error is till there and its telling me to put the semicolon back -.-
Answer by vxssmatty · Sep 05, 2011 at 04:36 AM
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
haha i know what was wrong... ok i've edited the post, try that... (EDIT: forgot a set of curly brackets)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Wierd error messages concerning audio play and pause? 1 Answer
Error CS1502 Help! 1 Answer
Script Error 4 Answers