Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Virepri · Mar 29, 2013 at 02:47 PM · javascriptontriggerenter

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;
 }
 }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Mar 29, 2013 at 03:37 PM 0
Share

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")) {
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Virepri · Mar 30, 2013 at 03:15 AM 0
Share

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.

avatar image Virepri · Mar 30, 2013 at 03:17 AM 1
Share

Fixed it! I honestly didn't think of that-- I'm new to Unity, so i'm figuring things out.

avatar image Chronos-L · Mar 30, 2013 at 03:22 AM 0
Share

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.

avatar image DryTear · Mar 30, 2013 at 03:23 AM 0
Share

if it helped you consider in check it as correct and at least an upvote

avatar image
0
Wiki

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Mar 29, 2013 at 05:15 PM 0
Share

No you should use CompareTag - it's faster

avatar image whydoidoit · Mar 29, 2013 at 05:16 PM 0
Share

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.

avatar image Virepri · Mar 29, 2013 at 10:09 PM 0
Share

Full script added.

avatar image DryTear · Mar 30, 2013 at 03:22 AM 0
Share

thank you mike - ill use that in my scripts

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trouble with a respawn script 1 Answer

how to stop every thing and enter into the OnTriggerEnter function? 0 Answers

Unity freezes when OnTriggerEnter occurs 3 Answers

OnTriggerEnter not detecting tag 2 Answers

Show message when gameobject hits other gameobject.. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges