- Home /
Help! OnTriggerEnter isn't working.
Here's my onTriggerEnter's javascript code:
function OnTriggerEnter (other : Collider) {
if (other.gameObject.CompareTag ("BuildGrist")) {
//Destroy grist object and collect grist
pickingUpGrist = true;
GristType = "BlueGrist";
Destroy(other.gameObject);
}
if (other.gameObject.CompareTag ("AmethystGrist")) {
//Destroy grist object and collect grist
pickingUpGrist = true;
GristType = "PurpleGrist";
Destroy(other.gameObject);
}
if (other.gameObject.CompareTag ("CaulkGrist")) {
//Destroy grist object and collect grist
pickingUpGrist = true;
GristType = "BlackGrist";
Destroy(other.gameObject);
}
if (other.gameObject.CompareTag ("MercuryGrist")) {
//Destroy grist object and collect grist
pickingUpGrist = true;
GristType = "GreyGrist";
Destroy(other.gameObject);
}
if (other.gameObject.CompareTag ("CobaltGrist")) {
//Destroy grist object and collect grist
pickingUpGrist = true;
GristType = "DBlueGrist";
Destroy(other.gameObject);
}
if (other.gameObject.CompareTag ("ChalkGrist")) {
//Destroy grist object and collect grist
pickingUpGrist = true;
GristType = "WhiteGrist";
Destroy(other.gameObject);
}
}
Unity keeps saying this: "Assets/Standard Assets/Scripts/Player Inventory/GristAmount.js(20,10): BCE0044: expecting (, found 'OnTriggerEnter'." Could someone please elaborate to me how to use onTriggerEnter or why this code isn't working?
EDIT: My function is on Line 20 of the source file.
EDIT: FULL SCRIPT:
#pragma strict
var BGristAmount = 1000;
var PGristAmount = 0;
var BlGristAmount = 0;
var GGristAmount = 0;
var DBGristAmount = 0;
var WGristAmount = 0;
var GrabAmount = 0;
var GristType = "";
var pickingUpGrist = false;
var PlayerLevel = 0;
function Start () {
}
function Update () {
GrabAmount = 20 * PlayerLevel;
Collider.OnCollisionEnter() {
if (gameObject.CompareTag ("BuildGrist")) {
//Destroy grist object and collect grist
pickingUpGrist = true;
GristType = "BlueGrist";
Destroy(other.gameObject);
}
if (gameObject.CompareTag ("AmethystGrist")) {
//Destroy grist object and collect grist
pickingUpGrist = true;
GristType = "PurpleGrist";
Destroy(other.gameObject);
}
if (gameObject.CompareTag ("CaulkGrist")) {
//Destroy grist object and collect grist
pickingUpGrist = true;
GristType = "BlackGrist";
Destroy(other.gameObject);
}
if (other.gameObject.CompareTag ("MercuryGrist")) {
//Destroy grist object and collect grist
pickingUpGrist = true;
GristType = "GreyGrist";
Destroy(other.gameObject);
}
if (other.gameObject.CompareTag ("CobaltGrist")) {
//Destroy grist object and collect grist
pickingUpGrist = true;
GristType = "DBlueGrist";
Destroy(other.gameObject);
}
if (other.gameObject.CompareTag ("ChalkGrist")) {
//Destroy grist object and collect grist
pickingUpGrist = true;
GristType = "WhiteGrist";
Destroy(other.gameObject);
}
}
if(pickingUpGrist){
switch(GristType){
case "BlueGrist":
BGristAmount = BGristAmount + GrabAmount;
pickingUpGrist = false;
break;
case "PurpleGrist":
PGristAmount = PGristAmount + GrabAmount;
pickingUpGrist = false;
break;
case "BlackGrist":
BlGristAmount = BlGristAmount + GrabAmount;
pickingUpGrist = false;
break;
case "GreyGrist":
GGristAmount = GGristAmount + GrabAmount;
pickingUpGrist = false;
break;
case "DBlueGrist":
DBGristAmount = DBGristAmount + GrabAmount;
pickingUpGrist = false;
break;
case "WhiteGrist":
WGristAmount = WGristAmount + GrabAmount;
pickingUpGrist = false;
break;
}
}
}
Your problem seems to be co$$anonymous$$g from what is before all that. Also, for a little efficiency, if it is not likely that any both of the cases inside the trigger method happen at once you could make the whole thing a if /else if:
if (other.gameObject.CompareTag ("BuildGrist")) {
}
else if (other.gameObject.CompareTag ("AmethystGrist")) {
}
else if (other.gameObject.CompareTag ("CaulkGrist")) {
}
Answer by Chronos-L · Mar 30, 2013 at 01:06 AM
Fix the error, by restructuring your code:
#pragma strict
...
function Start () {
}
function OnTriggerEnter( other : Collider ){
/* Keep all the if(gameObject.CompareTag)-statement here */
}
function Update () {
GrabAmount = 20 * PlayerLevel;
if(pickingUpGrist){
...
}
}
I do not know where you learn this from:
function Update() {
Collider.OnCollisionEnter() {
...
}
}
this snippet is incorrect. You should refer to the documentation if you have any doubts, almost all function in UnityEngine have a working example in the Unity Scripting documentation.
This is irrelevant to the question, but very important to you as a programmer, you have a poor naming habit here, look at this:
var GristType = "";
var pickingUpGrist = false;
var PlayerLevel = 0;
The pickingUpGrist is highlighted differently from GristType and PlayerLevel, you should name your variables according to the style of the pickingUpGrist (capitalize the first letter of all but the first word); your GristType and PlayerLevel is treated as a Class/Function, not variable in the code-formatting.
The style for GristType is not exactly wrong, but it is not recommended. You should adapt to the more common naming convention before your habit screws you up in the future.
Acctually, Lowercases help me sometimes-- It helps refer to various things. Also, thanks for the help, man! I think that fixed my code. I'm gonna try the fix now.
Fixed it! I honestly didn't think of that-- I'm new to Unity, so i'm figuring things out.
Using lowercase is a better na$$anonymous$$g convention, if you look at Unity's documentation, variables are in lowercase and Class/function are fully-capitalized.
if it helped you consider in check it as correct and at least an upvote
Answer by DryTear · Mar 29, 2013 at 03:58 PM
dont do:
other.gameObject.CompareTag
use:
other.gameObject.tag
post your full script because I need to see before the function starts because theres something there
But in this case it should be other.CompareTag (no need for the gameObject - that's slow) or a switch statement on tag. For a single comparision use CompareTag.
Your answer