- Home /
scripting colliders errors
hey guys newbie dev. here, I was testing a script that plays a sound when the player character collides with the door: function Start () { private var doorIsOpen : boolean = false; private var doorTimer : float = 0.0; private var currentDoor : GameObject; var doorOpenTime : float = 3.0; var doorOpenSound : AudioClip; var doorShutSound : AudioClip; } function Update () {
} function OnControllerColliderHit(hit : ControllerColliderHit){ if(hit.gameObject.tag == "playerDoor" && doorIsOpen == false){ function OnControllerColliderHit(hit: ControllerColliderHit){ if(hit.gameObject.tag == "playerDoor" && doorIsOpen == false){ OpenDoor(hit.gameObject); } } } } function OpenDoor(door : GameObject){ doorIsOpen = true; door.audio.PlayOneShot(doorOpenSound); }
And here's what console displays: PlayerCollisions.js(17,10):BCE0044: expecting (, found 'OnControllerCollideHit'. PlayerCollisions.js(17,34):BCE0043: Unexpected token: hit PlayerCollisions.js(17,61): UCE0001: ';' expected insert a semicolon at the end. PlayerCollisions.js(18,1): BCE0043: Unexpected token: if PlayerCollisions.js(18,62): UCE0001: ';' expected insert a semicolon at the end. PlayerCollisions.js((19,25): BCE0044: expecting : found ';'
Thank you in advance guys.
Please edit your code and format it using the "10101001" Button.
sorry about that the internet freezed while I was writing it so I had to copy/paste the question didn't notice it became like this so here: $$anonymous$$y script:
function Start () {
private var doorIsOpen : boolean = false;
private var doorTimer : float = 0.0;
private var currentDoor : GameObject;
var doorOpenTime : float = 3.0;
var doorOpenSound : AudioClip;
var doorShutSound : AudioClip;
}
function Update () {
}
function OnControllerColliderHit(hit : ControllerColliderHit){
if(hit.gameObject.tag == "playerDoor" && doorIsOpen == false){
function OnControllerColliderHit(hit: ControllerColliderHit){
if(hit.gameObject.tag == "playerDoor" && doorIsOpen == false){
OpenDoor(hit.gameObject);
}
}
}
}
function OpenDoor(door : GameObject){
doorIsOpen = true;
door.audio.PlayOneShot(doorOpenSound);
}
Consoles errors display:
PlayerCollisions.js(17,10):BCE0044: expecting (, found 'OnControllerCollideHit'.
PlayerCollisions.js(17,34):BCE0043: Unexpected token: hit.
PlayerCollisions.js(17,61): UCE0001: ';' expected insert a semicolon at the end.
PlayerCollisions.js(18,1): BCE0043: Unexpected token: if
PlayerCollisions.js(18,62): UCE0001: ';' expected insert a semicolon at the end.
PlayerCollisions.js((19,25): BCE0044: expecting : found ';'
No problem, I've actually already fixed the code and posted my answer below.. See if that works for you.
Answer by clunk47 · Dec 28, 2012 at 03:10 AM
You need to define your variables OUTSIDE of your functions, and you don't want to use a function twice. You had your OnControllerColliderHit function defined twice. There were a ton of brackets in the wrong place, a condition OUTSIDE of your IF statement... Then you had a function inside of another function. Try this, I hope this is what you were going for.
private var doorIsOpen : boolean = false;
private var doorTimer : float = 0.0;
private var currentDoor : GameObject;
var doorOpenTime : float = 3.0;
var doorOpenSound : AudioClip;
var doorShutSound : AudioClip;
function Start ()
{
}
function Update ()
{
}
function OnControllerColliderHit(hit : ControllerColliderHit)
{
if(hit.gameObject.tag == "playerDoor" && doorIsOpen == false)
{
OpenDoor(hit.gameObject);
}
}
function OpenDoor(door : GameObject)
{
doorIsOpen = true;
door.audio.PlayOneShot(doorOpenSound);
}
Not a problem at all man :)
If this resolves your issue, if you don't $$anonymous$$d accepting / voting up my answer, I would be greatly appreciative. I've voted up your question so now you have the ability to vote up if you wish :)